MathChE MATLAB ODE Part1
MathChE MATLAB ODE Part1
MathChE MATLAB ODE Part1
in Matlab
BP205
M.Tremont
1.30.2009
- Outline -
+,'+-./00(1-.2/3+'+45+67.8
• An ODE is an equation that contains one independent variable (e.g. time) 9
:;<
-=,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
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.
0/-,0/2/
*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
3. Run call_dstate.m
$TU T VTW
NTE QT)
-.2 +4'14
T HUST y(0) = 10
T
=@/,4
,0D32
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./+
steady_state =
• 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 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
Now solve on a time interval from 0 to 3000 with the above initial conditions.
! !
Create a scatter plot of y1 with time.
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
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
• Initial θ = π/3 dt
+4'14 `<;
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