Solving nonlinear scalar ode's
1 view (last 30 days)
Show older comments
I have to solve an ivp of the form: du/dt = f(t,u), u(t0) = u0, for a function u = u(t)?R for t=>t0, using Adams Bashforth 2nd order linear multistep method.
I have to write a function file with input t0 = initial time, tf = final time, u01=[u0; u1], n = number of time steps.
And output: t - is vector of length n+1 containing the times t0,t1,...,tn , and u - is a vector of length n+1 with the first element of u being u0 and with the (i+1) the element of u being the approximation ui for i=1,...,n.
So far i have: funtion [t,u]=ivpab2(t0,tf,u01,n) and i know that h=(tf-t0)/n and t(i)=t0+i*h The testing is to be done for the function u'=f(t,u)=-2*u+3*exp(-2*t)cos(3*t) which also needs to be implemented into this function file.
I would be extremely thankful for any help to how to even start with this.
Thank you.
0 Comments
Accepted Answer
Matt Tearle
on 21 Mar 2011
Given that you have to use AB2 (with, apparently, a fixed stepsize) I going to take a guess that this is a numerical analysis homework problem? (If not, use one of the built-in MATLAB linear multistep methods, such as ode113.)
For fixed stepsize, you can calculate all the t values immediately. Look at either the linspace function or the range operator (:).
You can and should make room for the u values before you start to calculate them. Look at zeros.
Set the first value of u from the initial condition.
Loop to fill in the rest of the values, based on the previous ones. Given that you know how many steps you're going to take, use a for-loop.
You can hard-code the rate equation inside your loop if you like. But if you want to be a bit fancier, use an anonymous function handle to define it. Something like f = @(x,y) -2*y + ... Then you can evaluate it anywhere later by simply calling f(t(k),y(k)) (or whatever).
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!