Single 1st Order ODE

13
Solving differential equations in MATLAB ode45 1 3/2/2009 Prepared by Dr.Waleed

description

How to solve ODE using matlab

Transcript of Single 1st Order ODE

Page 1: Single 1st Order ODE

Solving differential equations in MATLAB

ode45

1 3/2/2009 Prepared by Dr.Waleed

Page 2: Single 1st Order ODE

2

ode45(‘odefun’,tspan,y0)

tspan:A vector specifying the interval of integration, [t0,tf]

y0:A vector of initial conditions,[y’,y,..]

odefun:A function that evaluates the right side of the differential equations.

3/2/2009 Prepared by Dr.Waleed

1-Re-arrange equation to dy/dt=……….. 2-Specify time interval 3-Specify initial condition

Page 3: Single 1st Order ODE

Example1: a single 1st-order ode

Consider solving the following initial value problem:

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

function xprime=ex1(t,x) xprime=sin(t*x);

File name Function

name

10t0for 1, x(0),0)sin()(' txtx

3 3/2/2009 Prepared by Dr.Waleed

Page 4: Single 1st Order ODE

Step2:In the Matlab command window, type: clc

clear

[t,x] = ode45(‘ex1’,[0 ,10], [1]); plot(t,x)

Initial condition Time interval

4 3/2/2009 Prepared by Dr.Waleed

Time horizontal axis

x vertical axis

Matrix to save IN-OUT

Page 5: Single 1st Order ODE

Using ode45 to solve system of 1nd ODE

3/2/2009 Prepared by Dr.Waleed 5

Page 6: Single 1st Order ODE

Solving systems of 1st-order odes Consider solving the following system of system of ODE equations:

10t0 and 7.6)0(y ,1.5)0(:

)cos()sin( 0)cos()sin(

)sin()cos(0)sin()cos(

21

1

'

21

'

2

2

'

12

'

1

ywhere

tyytyy

tyytyy

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

6 3/2/2009 Prepared by Dr.Waleed

Page 7: Single 1st Order ODE

clc;clear tspan=[0, 10]; y0=[5.1, 6.7]; [t, z]=ode45(‘ex2’, tspan, y0); plot(t, z)

Create new file and type the following:

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.

7 3/2/2009 Prepared by Dr.Waleed

Page 8: Single 1st Order ODE

Using ode45 to solve system of 2nd ODE

3/2/2009 Prepared by Dr.Waleed 8

Page 9: Single 1st Order ODE

Solving 2nd-order ode Consider solving the following 2nd order ODE:

5t0 and 1)0(y ,0)0(:

092

'

'''

ywhere

ytyy

1)0( and 0)0(z

:becomecondition initial and

, ......eq2 29z

....eq1 z

:asn can writte problem

,yz andy z

21

21

'

2

2

'

1

'

21

z

tzz

z

the

Let

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];

9 3/2/2009 Prepared by Dr.Waleed

State Variables

Put it in Matrix Form?

State Variables

Put it in Matrix Form?

''' 29 tyyy

Page 10: Single 1st Order ODE

Create new file, type:

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

10 3/2/2009 Prepared by Dr.Waleed

Page 11: Single 1st Order ODE

System of 2nd ODE

3/2/2009 Prepared by Dr.Waleed 11

22322

2121111 )(

xBxKTxM

xxKxKxM

𝐼𝑛𝑖𝑡𝑖𝑎𝑙 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛𝑠 𝑎𝑟𝑒 𝑓𝑜𝑢𝑟: 𝑥1′ 0 = ⋯ , 𝑥1 0 = ⋯ 𝑥2′ 0 = ⋯ , x2 0 = ⋯

Page 12: Single 1st Order ODE

3/2/2009 Prepared by Dr.Waleed 12

24

23

12

11

xz

xz

xz

xz

Page 13: Single 1st Order ODE

3/2/2009 Prepared by Dr.Waleed 13

42

12

2332

24

43

31

2121

12

21

)(1

)(1

zM

Bz

M

KzKK

Mz

zz

zM

KzKK

Mz

zz

)4()1()3()(1

)4(

)3()1()(1

)2(

22

232

24

3

1

221

12

1

zM

Bz

M

KzKK

MEq

zEq

zM

KzKK

MEq

zEq

In Matlab form