0% found this document useful (0 votes)
59 views

MathChE MATLAB ODE Part1

This document discusses solving ordinary differential equations (ODEs) numerically in MATLAB. It begins by explaining that an ODE contains one independent variable (e.g. time) and one or more derivatives with respect to that variable. MATLAB has functions like ode45 that can be used to solve initial value problems for ODEs. The document then covers: (1) defining an ODE function in an M-file, (2) solving first-order ODEs, and (3) solving systems of first-order ODEs, like the van der Pol equations example given. Functions are created separately for the call and ODE definitions when solving systems of ODEs in MATLAB.

Uploaded by

Arm Charoenkool
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

MathChE MATLAB ODE Part1

This document discusses solving ordinary differential equations (ODEs) numerically in MATLAB. It begins by explaining that an ODE contains one independent variable (e.g. time) and one or more derivatives with respect to that variable. MATLAB has functions like ode45 that can be used to solve initial value problems for ODEs. The document then covers: (1) defining an ODE function in an M-file, (2) solving first-order ODEs, and (3) solving systems of first-order ODEs, like the van der Pol equations example given. Functions are created separately for the call and ODE definitions when solving systems of ODEs in MATLAB.

Uploaded by

Arm Charoenkool
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Solving ODEs

in Matlab
BP205
M.Tremont
1.30.2009
- Outline -

I. Defining an ODE function in an M-file


II. Solving first-order ODEs
III. Solving systems of first-order ODEs
IV. Solving higher order ODEs
What
Numerical areare
methods weused
doing when
to solve initial value
problemsnumerically
where it is difficult to obtain
solving exact solutions
ODE’s? !
"#"$%&'((&)'*

+,'+-./00(1-.2/3+'+45+67.8
• An ODE is an equation that contains one independent variable (e.g. time) 9

and one or more derivatives with respect to that independent variable.


• In the time domain, ODEs are initial-value problems, so all the conditions
are specified at the initial time t = 0.

:;<
-=,4.2+4'14+<;>',==,0'?--
dy t (.+,+-..+
/0/@A2,B/@ 3. ?
&3714,B/@ = y(0) = 1 9 /

712C+' 712C+' dt y
>/@BC/.
?
;3@14

!
y(t) = t 2 + 1
;8/B2 .+@32,+0
!
!
• Matlab has several different functions (built-ins) for the numerical
solution of ODEs. !
These solvers can be used with the following syntax:
,0'1D10'102 6/4,/(@1

E [outputs] = function_handle(inputs) D,0,0=/4,0/


[t,state] = solver(@dstate,tspan,ICs,options)
2@+-+6+.A 2++2C=,.C

An array. The solution of Matlab algorithm Handle for function Vector that specifiecs the A vector of the
the ODE (the values of (e.g., ode45, containing the interval of the solution initial conditions
the state at every time). ode23) derivatives (e.g., [t0:5:tf]) for the system
(row or column)
What are we doing when
numerically solving ODE’s?
F 6/,0 71 ?

y(t)
We know t0 and y(t0)
y!
* * y(t0) and we know the slope of
* * y(t), dy/dt =f(t,y).
* * *
t0 t
We don’t know y(t) for any
values of t other than t0.

Integrators compute nearby value of y(t) using


what we already know and repeat.

Higher order numerical methods reduce error at the cost of speed:


• Euler’s Method - 1st order expansion
• Midpoint method - 2nd order expansion
• Runge-Kutta - 4th order expansion
G ! H (4+I 0+7,0/@ J( 0+2+4,+3.
(3,@29,0 =0 ?

0/-,0/2/

Solver Accuracy Description


K ode45 Medium This should be the
L+I
first solver you try Runge-Kutta
(4,5) formula
ode23 Low Less accurate
than ode45

ode113 Low to high For


4/0-1 .+M+340
computationally
III?70'+.,0+7 intensive
NJ" O problems
K ode15s Low to Use if ode45
)PQR++'1HS medium fails because the
L4+I0,/0 problem is stiff*

*No precise definition of stiffness, but the main idea is that the equation
includes some terms that can lead to rapid variation in the solution.
Defining an ODE function in an M-file

+32D32 A
=+ = 0

[t,state] = ode45(@dstate,tspan,ICs,options)
III

1. Define tspan, ICs and options in one file (e.g.


call_dstate.m), which sets up ode45

2. Define your constants and derivatives in another file


(e.g. dstate.m) or as a function dstate within the call
file

3. Run call_dstate.m

4. Analyze the results as a plot of state vs. t


II. Solving first-order ODEs
dy
= y'(t) = "y(t) # $y(t) 2
Example: dt
I,0

$TU T VTW

NTE QT)
-.2 +4'14
T HUST y(0) = 10
T

=@/,4
,0D32

1 fu nct ion [t,y ]X = cal l_d stat e()


2-
!
ts pan = [0 9]; % set tim e inte rval
9

3- y0 = 10; % set ini !tia l condi tio n


4 % ds tat e ev alu ates r. h.s . of th e ode
5- [t ,y] = ode4 5( @d sta te ,ts pan ,y 0);

Y
6- pl ot( t,y)
7- di sp ([ t,y ]) % dis play s t an d y( t)
8 fu nct ion dy dt = ds tat e (t ,y)V?
9- al pha =2; gam ma= 0.00 01;
10- dy dt = alp ha* y- gam ma *y ^2;
11- en d
12- en d

Save as call_dstate.m in some directory, and cd to that directory in the matlab GUI
Matlab ode45’s numerical solution

dy
= y'(t) = "y(t) # $y(t) 2
dt
y(0) = 10

At t = 9, have we reached
! steady state?
! $
limt"># y(t) = = 20,000
% DC0/,2+,/,071.+0./+

EDU>> [t, y] = call_dstate;


From the command line: ! steady_state = y(end)
EDU>>

steady_state =

1.9999e+04 Z /0 7/2C @/(


III. Solving systems of first-order ODEs

van der Pol equations in relaxation oscillation:


dy1 y1 (0) = 0
= y2
dt
dy 2
= 1000(1" y12 )y 2 " y1 y 2 (0) = 1
dt

! O %% [37. \ L/C0 0/C E +,+ C+34 +0 I

• This is a system of ODEs because we have more than one derivative with
respect!to our independent variable,!time.

• This is a stiff system because the limit cycle has portions where the
solution components change slowly alternating with regions of very sharp
change - so we will need ode15s. 9

• This is a example from mathworks, a great resource @ mathworks.com or


the software manual.

• This time we’ll create separate files for the call function (call_osc.m) and
the ode function (osc.m)
III. Solving systems of first-order ODEs

van der Pol equations in relaxation oscillation:


dy1 y1 (0) = 0
= y2
dt
dy 2
= 1000(1" y12 )y 2 " y1 y 2 (0) = 1
dt

To simulate this system, create a function osc containing the equations.


! !
Method 1: preallocate space in a column vector, and fill with derivative functions
1 function dydt = osc(t,y)
2- dydt = zeros(2,1); % this creates an empty column
3 %vector that you can fill with your two derivatives:
4- dydt(1) = y(2);
5- dydt(2) = 1000*(1 - y(1)^2)*y(2) - y(1);
6 %In this case, y(1) is y1 and y(2) is y2, and dydt(1)
7 %is dy1/dt and dydt(2) is dy2/dt.
8- end
[+,0 = 0 ?

Save as osc.m in the same directory as before.


III. Solving systems of first-order ODEs

van der Pol equations in relaxation oscillation:


dy1 y1 (0) = 0
= y2
dt
dy 2
= 1000(1" y12 )y 2 " y1 y 2 (0) = 1
dt

To simulate this system, create a function osc containing the equations.


! !
Method 2: vectorize the differential functions
] .C,014. ]

1 function dydt = osc(t,y)


2- dydt = [y(2)
3 1000*(1 - y(1)^2)*y(2) - y(1)];
4 %Still y(1) is y1 and y(2) is y2, and dydt(1)
5 %is dy1/dt and dydt(2) is dy2/dt.
6- end

Save as osc.m in the same directory as before.


III. Solving systems of first-order ODEs

van der Pol equations in relaxation oscillation:


dy1 y1 (0) = 2
= y2
dt
dy 2
= 1000(1" y12 )y 2 " y1 y 2 (0) = 0
dt

Now solve on a time interval from 0 to 3000 with the above initial conditions.
! !
Create a scatter plot of y1 with time.

1 function [T,Y] = call_osc()


2- tspan = [0 3000];
3- y1_0 = 2;
4- y2_0 = 0;
5- [T,Y] = ode15s(@osc,tspan,[y1_0 y2_0]);
6- plot(T,Y(:,1),'o')
7-end G
I4+0- D@+2214. / /. 0+I

Save as call_osc.m in the same directory as before.


Plot of numerical solution

van der Pol equations in


relaxation oscillation:
dy1
= y2
dt
dy 2
= 1000(1" y12 )y 2 " y1
dt

y1 (0) = 2
!
y 2 (0) = 0

!
IV. Solving higher order ODEs

Simple pendulum:
d 2!
ML 2 = " MG sin !
dt B+0.2/02
/
7/
2
d! Z
G
2
= " sin !
dt L G "I

G 4,7

• Second order non-linear ODE


• Convert the 2nd order ODE to standard form: MG
9
^+7+,+3.,/0
_41 z1 = ", z2 = d" /dt
dz1
B+0B1D2 O =30'14.
C,-C14dt
= z2 `<;
+4'14
.A.217
Z
+= U.2 +4'14 `<;

dz2 G
V0' +4'14 2+
V ,2 = #
+4'14 ^+25
sin(z[/7(
1)
dt L
W4' +4'14 Z . ,2 +4'14 ^+2. '37(
Non-linear pendulum function file

• G = 9.8 m/s z1 = ", z2 = d" /dt


• L=2m
dz1
• Time 0 to 2π = z2
X Oa
.217 +=

• Initial θ = π/3 dt
+4'14 `<;

• Try ode23 dz2 G


= # sin(z1 )
• Plot θ with time dt L

1 function [] = call_pend()
2- tspan=[0 2*pi]; % set time interval
3- z0=[pi/3,0]; % !set initial conditions
4- [t,z]=ode23(@pend,tspan,z0);
5- plot(t,z(:,1))
6 function dzdt = pend(t,z)
7- G=9.8; L=2; % set constants
8- z1=z(1); % get z1
9- z2=z(2); % get z2
10- dzdt = [z2 ; -G/L*sin(z1);];
11- end
12- end

You might also like