

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


This routine uses a variable step Runge-Kutta Method to solve differential equations numerically. The syntax for ode45 for first order differential equations and that for second order differential equations are basically the same. However, the .m files are quite different.
{ y ′^ = f (t, y) y(t 0 ) = y 0
A. Create a .m file for f (t, y) (see the tutorial on numerical methods and m files on how to do this). Save file as, for example, yp.m. B. Basic syntax for ode45. At a Matlab prompt type :
[t,y]=ode45(’yp’,[t0,tf],y0); (your version of ode45 may not require brackets around t0, tf)
yp = the .m file of the function f (t, y) saved as yp.m t0, tf = initial and terminal values of t y0 = initial value of y at t 0
C. For example, to numerically solve
{ t^2 y ′^ = y + 3t y(1) = − 2 over the interval 1 ≤ t ≤ 4 :
t^2
(y + 3t).
[t,y]=ode45(’yp’,[1,4],-2); (your version of ode45 may not require backets around [1,4])
y′′^ + p(t) y ′^ + q(t) y = g(t) y(t 0 ) = y 0 y ′(t 0 ) = y 1
A. First convert 2nd^ order equation above to an equivalent system of 1st^ order equations. Let x 1 = y, x 2 = y ′^ : { x′ 1 = x 2 x′ 2 = −q(t) x 1 − p(t) x 2 + g(t) , where x 1 (t 0 ) = y 0 , x 2 (t 0 ) = y 1.
B. Create and save a .m file which will return a vector-valued function. This is a little tricky so here is a specific example. Suppose the system is as below and 0 ≤ t ≤ 4 { x′ 1 = x 2 x′ 2 = −t x 1 + et^ x 2 + 3 sin 2t , where x 1 (0) = 2, x 2 (0) = 8.
function xp=F(t,x) xp=zeros(2,1); % since output must be a column vector xp(1)=x(2); xp(2)=-tx(1)+exp(t)x(2)+3sin(2t); % don’t forget ; after each line
[t,x]=ode45(’F’,[t0,tf],[x10,x20]);
F = the .m file of the vector-function saved as above t0, tf = initial and terminal values of t x10 = initial value of x 1 at t 0 : x10 = x 1 (t 0 ) x20 = initial value of x 2 at t 0 : x20 = x 2 (t 0 ) The example above becomes :[t,x]=ode45(’F’,[0,4],[2,8]);
[t,x(:,1)]
To plot the solution on a graph t vs y, type : plot(t,x(:,1))
(This is because the vector x has 1st^ component x 1 = y and 2nd^ component x 2 = y ′.)