MATLAB
ODE Solver
DI Johannes Martinek
MATLAB: differential equations I
can only solve first order DE (FODE)
Rewrite your problem to a system of FODE
Write a function (odefun) that describes your system
Call solver
odefun:
System-state described using a state vector y
e.g. y = [x, v]
Odefun returns derivative:
dy/dt = [dx/dt, dv/dt], all in terms of y
(current state)
MATLAB: differential equations II
various solvers exist
look for suitable solver
faster (for stiffer/non-stiff/.., DE)
more accurate in tricky situations (precision)
different methods/approaches implemented
MATLAB: differential equations III
One syntax:
[t,y] =
solver(@odefun,tspan,y0,options,p1,...,pn)
dydt = odefun(t,y)
tspan = [tstart, ..., tend]
Y0 : initial conditions
tspan: time points
y: solution array
MATLAB: ODE example
Damped driven harmonic oscillator
y‘‘ + a1 y‘ + a2 y + a3 sin( a4 t) = 0
Rewrite to system of first order ODEs:
y1‘ = y2
y2‘ = - [a1 y1‘ + a2 y1 + a3 sin( a4 t) ]
y = [y1, y2] = [pos, speed]
MATLAB: ODE example II
Plotting results
Freier Fall (allgemein)
m • ay = ∑Fy
y
m • (-a) = Fw – m • g
Fw Fw = A•cw•ρ/2•v2
m
a a = g – A/m•cw•ρ/2•v2
v
-a = s‘‘ -v = s‘
h m•g
s‘‘ = -g + A/m•cw•ρ/2•(s‘)2
0 nichtlineare DGL 2.Ord.!!!
Freier Fall - ODE
1) Rewrite the problem as a system of first-order ODEs
1) s‘‘ = -g, s‘ = v => v‘ = -g => s = y(1), v = y(2)
2) y(1)‘ = y(2), y(2)‘ = -g
2) Code the system of first-order ODEs
1) function dydt = ffdgl(t,y)
2) dydt = [y(2); -g];
3) Apply a solver to the problem
1) [t,y] = ode45(@ffdgl,[0 1],[10; 0]);
2) t = 0..1s, y(1) = 10m, y(2) = 0m/s
Schiefer Wurf (allgemein)
Bewegungsgleichung
d 2x
m 2 0
dt
2
d y
m 2 m g 0
dt
Schiefer Wurf - ODE
1) Rewrite the problem as a system of first-order ODEs
d 2x d2y x ~
x x0
m 2 0 x 0 m 2 m g 0 y g
dt
~x x
dt
~y y
x x
~ v0 cos
y ~
y y0
~x x 0 ~y y g
y y
~ v0 sin
2) Code the system of first-order ODEs
function dydt = wurfdgl(t,y)
dydt = [y(2); 0; y(4); -9.81]
3) Apply a solver to the problem
[t,y] = ode45(@wurfdgl,[0,3],[0,10*cos(pi/4),10,10*sin(pi/4)]);
DGL [t_start, t_end]