Lecture5 Body
Lecture5 Body
Shan He
Outline of Topics
What is ODE
Definition of ODE
F (x, y , y 0 , . . . , y (n) ) = 0
Ordinary Differential Equations (ODE) in MATLAB
Concepts about ODE
Solution to ODE
1 tspan = [0 10];
2 y0 = [1 0];
3 [X,Y] = ode45(@simple_ode,tspan,y0);
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB
F (x, y , y 0 , . . . , y (n) ) = 0
Methods:
I We will use a second order ODE as an example:
(
x 0 = −ye (−t/5) + y 0 e (−t/5) + 1
y 00 = −2 sin(t)
Methods:
I Step 3: Substituting the second order ODE with z and z 0 :
0 (−t/5) + ze (−t/5) + 1
x = −ye
z 0 = −2 sin(t)
0
y =z
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB
Methods:
I Step 4: Write a MATLAB function ho ode.m to define the
ODE:
1 function dy = high_order_ode_example(t,x)
2 % x(1) = x
3 % x(2) = y
4 % x(3) = z
5 dy = [-x(2) * exp(-t/5) + ...
6 x(3) * exp(-t/5) + 1;
7 x(3);
8 -2*sin(t)]
9 end
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB
Methods:
I Step 5: evaluate the system of equations using ODE45:
1 t0 = 5; % Start time
2 tf = 20; % Stop time
3 x0 = [1 -1 3] % Initial conditions
4 [t,s] = ode45(@ho_ode,[t0,tf],x0);
5 x = s(:,1);
6 y = s(:,2);
7 z = s(:,3);
8 plot(t,s);
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics
y0 = y2 − y3
y (0) = τ
2
0≤t≤
τ
I If we weren’t concerned with efficiency of ODE solver, we
wouldn’t be concerned about stiffness.
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics
I Call the ODE solver and pass the parameter to the ODE
function:
1 tspan = [0 10];
2 initvalue = [1 0];
3 option = [];
4 for ii=-10:4
5 A = [1 ii; 2 -1];
6 [t,y] = ode45(@ode_pv,...
7 tspan,initvalue,option,A);
8 hold on;
9 plot(t, y);
10 end
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics
Events
I Recall syntax of the ODE solvers:
[T,Y] = ode**(@odefun,tspan,y0,options)
I We generally assume tspan is known, e.g.,
t0 ≤ t ≤ tfinal
I But sometimes it is also important to determine tfinal .
I Example: a ball is falling because of gravity, when does it hit
the ground?
I Equations:
y 00 = g = −9.8
where y (t) is the height of the object at time t
I The question: for what t does y (t) = 0? We can use events.
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics
Define events
I What we have introduced are all for Initial Value Problems for
ODEs.
I To solve Boundary Value Problems: bvp4c.
Tutorial.
I To solve Delay ODEs: dde23.
Tutorial.
I To solve Stochastic ODEs: MATLAB SDE Toolbox.
Tutorial.