You are on page 1of 13

Solving differential equations in

MATLAB

ode45

3/2/2009

Prepared by Dr.Waleed

ode45(odefun,tspan,y0)
odefun:A function that evaluates the right side of the differential equations.
tspan:A vector specifying the interval of integration, [t0,tf]
y0:A vector of initial conditions,[y,y,..]
1-Re-arrange equation to dy/dt=..
2-Specify time interval
3-Specify initial condition

3/2/2009

Prepared by
2 Dr.Waleed

Example1: a single 1st-order ode


Consider solving the following initial value problem:

x ' (t ) sin(tx) 0,

x(0) 1, for 0 t 10

Step1 :Create the following .m file called ex1.m:


Function
name

File name

function xprime=ex1(t,x)
xprime=sin(t*x);
3/2/2009

Prepared by Dr.Waleed

Step2:In the Matlab command window, type:


Matrix
to save
IN-OUT

clc
clear Time interval
[t,x] = ode45(ex1,[0 ,10], [1]);
x
plot(t,x)

Initial condition

vertical axis

Time
horizontal axis

3/2/2009

Prepared by Dr.Waleed

Using ode45 to solve system of


1nd ODE

3/2/2009

Prepared by Dr.Waleed

Solving systems of 1st-order odes


Consider solving the following system of system of ODE equations:

y1' cos( y2 ) sin(t ) 0 y1' cos( y2 ) sin(t )


y sin( y1 ) cos(t ) 0 y sin( y1 ) cos(t )
'
2

'
2

where : y1 (0) 5.1, y 2 (0) 6.7 and 0 t 10


Step1 :Create the following .m file called ex2.m:
function yprime=ex2(t,z)
Eq1= cos(z(2))+sin(t);
Eq2= sin(z(1))-cos(t);
yprime=[Eq1; Eq2];
or

yprime=[cos(z(2))+sin(t);sin(z(1))-cos(t)];
Now z and yprime are both column vectors

3/2/2009

Prepared by Dr.Waleed

Create new file and type the following:


clc;clear
tspan=[0, 10];
y0=[5.1, 6.7];
[t, z]=ode45(ex2, tspan, y0);
plot(t, z)
Of course, you can save the above
commands to any name .m file and
type the name of the file in the
command window to run it.

3/2/2009

Prepared by Dr.Waleed

Using ode45 to solve system of


2nd ODE

3/2/2009

Prepared by Dr.Waleed

Solving 2nd-order ode


Consider solving the following 2nd order ODE:

y '' 2ty ' 9 y 0

where : y (0) 0, y ' (0) 1 and 0 t 5


Let z1 y and z 2 y ' ,
the problem can written as :
z1' z 2

....eq1

z '2 9 z1 2tz 2

......eq2 ,

State Variables
Put it in Matrix
Form?

y '' 9 y 2ty '

and initial condition become :


z1 (0) 0 and z 2 (0) 1

Step1 :Create the following .m file called ex3.m:


function zprime=ex3(t,z)
Eq1=z(2) ; Eq2=-9*z(1)-2*t*z(2);
zprime=[Eq1;Eq2];
3/2/2009

Prepared by Dr.Waleed

Create new file, type:

clc;clear
z0=[0,1];
tspan=[0,5];
[t,z]=ode45(ex3, tspan,z0);
plot(t,z);

3/2/2009

Prepared by Dr.Waleed

10

System of 2nd ODE


M 1 x1 K1 x1 K 2 ( x1 x2 )
M 2 x2 T K 3 x2 Bx 2

: 1 0 = , 1 0 =
2 0 = , x2 0 =

3/2/2009

Prepared by Dr.Waleed

11

z1 x1
z 2 x1
z 3 x2
z 4 x 2

3/2/2009

Prepared by Dr.Waleed

12

z1 z 2
K2
1
z 2
( K1 K 2 ) z1
z3
M1
M1
z3 z 4
K2
1
B
z 4
( K 2 K 3 ) z3
z1
z4
M2
M2
M2
In Matlab form

Eq1 z (2)
K2
1
Eq 2
( K1 K 2 ) z (1)
z (3)
M1
M1
Eq3 z (4)
K2
1
B
Eq 4
( K 2 K 3 ) z (3)
z (1)
z (4)
M2
M2
M2
3/2/2009

Prepared by Dr.Waleed

13

You might also like