next up previous
Next: About this document Up: MATLAB summary Previous: If-then-else statements.

Solution of ODEs


To solve dy/dt=t, y(0)=1, type

>> small;

where small.m contains:

t0=0;
tfinal=10;
yin=1;
tstep=0.2;
tspan=t0:tstep:tfinal;
[t,yode]=ode45('small_rhs',tspan,yin);    % variable step RK4/5
plot(t,yode)

(This routine outputs the solution at t0,t0+tstep, ..., tfinal but calculates the solution at whatever variable step-size is required.)


In small_rhs.m we have the right hand side:

function rhs=small_rhs(t,y)
rhs=t;

note that the FILENAME, not the function name, is used in the call to ode45.m


To plot the exact solution to dy/dt=t, y(0)=1 and compare with the numerical solution calculated above:
texact=0:1:10;
solution=0.5*texact.^2 + 1;
plot(texact,solution,'g+',t,yode,'r')



Charlie Macaskill
Fri Mar 5 16:05:28 EST 1999

Copyright © University of Sydney 2003