solving a linear initial value problem in MATLAB
7 views (last 30 days)
Show older comments
I have to solve an equation of the form: u''' = au'' + bu' + cu, u(t0) = u0, u'(t0) = ud0, u''(t0) = uddo. I have to write a function file with input u_vec: = [u0 ud0 udd0]' abc_vec: =[a b c]', t0 = initial time, tf = final time, n= number of time steps. And output: u - is a row vector of size n+1 with the first entry of u being u0 and with the (i+1) the entry of u being ui =u(ti) where i =1:100. And t = is a row vector of length n+1 containing t0....tn.
I am just stuck at where to even start with this.
Also I have to write a script file which will produce a graph of u against t. I have not a clue about how to.
I would be extremely thankful for any help.
Thank you.
0 Comments
Answers (4)
Jan
on 3 Mar 2011
At first you have to convert the system to degree 1 - this is implied in "u_vec = [u, ud, udd]" already. Then you need an integrator. Try ODE45 and check the examples include in the help and doc.
0 Comments
Matt Tearle
on 3 Mar 2011
- Convert the third-order equation into a (3-D) first-order system
- Write a function that defines the rate equations. The system should now look like z' = f(t,z), where z = [u, u', u'']. Write your function file so that it takes t and a vector z as inputs, and returns a vector dz that represents the derivatives ([u', u'', u'''])
- Use ode45 to solve the system, giving a function handle to you function from step 2 as input
- Extract the appropriate component of the solution returned by ode45 to plot as a function of t (also returned by ode45).
Matt Tearle
on 3 Mar 2011
Oh, sorry, it looks like Jan & I both misread/misinterpreted your questions. You're supposed to write your own ode integrator? OK, I assume, then, that this is some kind of homework problem. If not, the short answer is: don't -- use ode45 instead.
So your function above is a reasonable start. A few things, though:
- You don't need a loop to calculate t. Use the range (:) operator or the linspace function.
- MATLAB uses standard mathematical order of operations, so h = tf-t0/n will calculate t0/n first, then subtract from tf
- You don't need to extract x, y, and z individually to make M. You can use fliplr and concatenation with abc_vec directly.
Next you need to write a loop to do the actual integration, using a method of your choosing. Make sure you preallocate space for the result u before you do so; then in the loop, calculate u(k) based on u(k-1). (This is much more efficient that calculating the kth u and appending it to the ones you have so far.)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!