ordinary differential equations (odes)
DESCRIPTION
Ordinary Differential Equations (ODEs). Daniel Baur ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften ETH Hönggerberg / HCI F128 – Zürich E-Mail: [email protected] http://www.morbidelli-group.ethz.ch/education/index. Problem Definition. - PowerPoint PPT PresentationTRANSCRIPT
1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Ordinary Differential Equations (ODEs)
d ( )( , )
d
y tf t y
t
Daniel Baur
ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften
ETH Hönggerberg / HCI F128 – Zürich
E-Mail: [email protected]
http://www.morbidelli-group.ethz.ch/education/index
2Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Problem Definition
We are going to look at initial value problems of explicit ODE systems, which means that they can be cast in the following form
0 0
d ( )( , )
d( )
y tf t y
ty t y
Higher Order ODEs Higher order explicit ODEs can be cast into the first order
form by using the following «trick»
Therefore, a system of ODEs of order n can be reduced to first order by adding n-1 variables and equations
Example:
3Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
21
12
d( , )
d
yf t y
t
12
21 1
2
d:
d
d dd
d d d
yy
t
y y
t t t
12
22 1
12
d
d
d d( , )
d d
yy
t
y yf t y
t t
21
12
d
d
ycy
t
12
21 1
2
d:
d
d dd
d d d
yy
t
y y
t t t
12
21
d
dd
d
yy
ty
cyt
4Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
ODEs as vector field
Since we know the derivatives as function of time and solution, we can calculate them at every point in time and space
2d
d
yy y
t
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
0.2
0.4
0.6
0.8
1
1.2
Independent variable
Dep
ende
nt v
aria
ble
0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8
0.75
0.8
0.85
0.9
0.95
1
Independent variable
Dep
ende
nt v
aria
ble
5Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
ODE systems as vector fields
We can do the same for ODEs systems with two variables, at a certain time point
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.2
0.4
0.6
0.8
1
Variable 1
Var
iabl
e 2
0.65 0.7 0.75 0.8 0.85 0.9 0.950.6
0.65
0.7
0.75
0.8
Variable 1
Var
iabl
e 2
d1 0.45 0.45
dd
0.45 0.33d
xxy x
ty
xy yt
6Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Example: A 1-D First Order ODE
Consider the following initial value problem
This ODE and its solution follow a general form(Johann Bernoulli, Basel, 17th – 18th century)
( )2
(0) 1
dy ty t
dty
( )d ( )d
d ( )( ) ( ) ( )
d
( ) e ( ) e dp s s p s s
y tr t p t y t
t
y t k r t t
21( ) 3e 2 1
4ty t t
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 11
2
3
4
5
6
7
t
y
7Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Some Nomenclature
On the next slide, the following notation will be used
Value of at point
Value of the derivative of at point
Integration step
n n
n n
y y t
y y t
h
8Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Numerical Solution of a 1-D First Order ODE
Since we know the first derivative, Taylor expansion suggests itself as a first approach
This is known as the Euler method, named after Leonhard Euler (Basel, 18th century)
( )2
(0) 1
dy ty t
dty
21( ) 3e 2 1
4ty t t
2
1 1 1
2 2
d( )
d
( , )
n
n n n n n nt
n n n n n
yy y t t O t t
t
y hy O h y h f t y O h
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 11
2
3
4
5
6
7
t
x
Euler Method
Exact Solution
9Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
How does Matlab do it? Non-Stiff Solvers
ode45: Most general solver, best to try first; Uses an explicit Runge-Kutta pair (Dormand-Prince, p = 4 and 5)
ode23: More efficient than ode45 at crude tolerances, better with moderate stiffness; Uses an explicit Runge-Kutta pair (Bogacki-Shampine, p = 2 and 3).
ode113: Better at stringent tolerances and when the ODE function file is very expensive to evaluate; Uses a variable order Adams-Bashforth-Moulton method
10Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
How does Matlab do it? Stiff Solvers
ode15s: Use this if ode45 fails or is very inefficient and you suspect that the problem is stiff; Uses multi-step numerical differentiation formulas (approximates the derivative in the next point by extrapolation from the previous points)
ode23s may be more efficient than ode15s at crude tolerances and for some problems (especially if there are largely different time-scales / dynamics involved); Uses a modified Rosenbrock formula of order 2 (one-step method)
11Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Matlab Syntax Hints
All ODE-solvers use the syntax[T, Y] = ode45(ode_fun, tspan, y0, …); ode_fun is a function handle taking two inputs, a scalar t (current
time point) and a vector y (current function values), and returning as output a vector of the derivatives dy/dt in these points
tspan is either a two component vector [tstart, tend] or a vector of time points where the solution is needed [tstart, t1, t2, ...]
y0 are the values of y at tstart
Use parametrizing functions to pass more arguments to ode_fun if neededf_new = @(t,y)ode_fun(t, y, k, p);
This can be done directly in the solver call [T, Y] = ode45(@(t,y)ode_fun(t, y, k, p), tspan, y0);
12Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Exercise 1: Damped harmonic oscillator
A mass on a string is subject to a force when out of equilibrium
According to Newton’s second law (including friction), the resulting movement follows the solution to an ODE:
m
y
F
2
2
d d
d d
y ym ky at t
Spring force
Friction
13Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Assignment 1
1. Rewrite the motion equation for the damped harmonic oscillator as an explicit, first order ODE system
2. Use ode45 to solve the problem Set up a function file to calculate the derivatives, its header should
read something likefunction dy = harm_osc(t, y, m, k, a)
Set m = 1; k = 10; a = 0.5; y0 = 1; dy/dt0 = 0; Use tSpan = [0, 20];
3. Plot the position of the mass y against time
4. Plot the speed of the mass dy/dt against time
14Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Exercise 2: Radioactive decay
A decaying radioactive element changes its concentration according to the ODE
where k is the decay constant The analytical solution to this problem reads
d
d
yky
t
0 expy t y kt
15Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Assignment 21. Plot the behavior of the radioactive decay problem as a
vector field in the y vs t plane Find online the function vector_field.m. It plots the derivatives of
first order ODEs in different space and time points Plot the vector field for y values between 0 and 1, time between 0
and 2 and k = 1. Can you see what a solver has to do? Is it possible to switch from one trajectory in the vector field to
another? What follows for the uniqueness of the solutions?
2. Implement the forward Euler method in a function file The file header should read something likefunction [t,y] = eulerForward(f, t0, tend, y0, h)
Use y0 = 1, k = 1 and h = 0.1 to solve the radioactive decay problem from t0 = 0 to tEnd = 10 and plot the solution together with the analytical solution.
What happens if you increase the step size h? What happens if you increase the step size above 2?
16Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Exercise 3: Lotka-Volterra equations
Predator-prey dynamics can be modeled with a simple ODE system:
where x is the number of prey, y is the number of predators and a, b, c, d are parameters
d
dd
d
xx a by
ty
y c dxt
17Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers
Assignment 3
1. Plot the behavior of the predator-prey system as a vector field in the x vs y plane Find online the function vector_field2D.m which plots this vector
field Plot x and y between 0 and 1, and use a = c = 0.5 and b = d = 1 Without solving the ODEs, what can you say about the solution
behavior just by looking at the trajectories?
2. Solve the system using ode45, and x0 = y0 = 0.3 as initial values, and tSpan = [0, 50]; Plot x and y against t. Can you observe what you predicted in 1?