Solving Initial Value Problems: Jake Blanchard University of Wisconsin - Madison Spring 2008
Solving Initial Value Problems: Jake Blanchard University of Wisconsin - Madison Spring 2008
Jake Blanchard
University of Wisconsin - Madison
Spring 2008
Example Problem
Consider an 80 kg paratrooper falling
from 600 meters.
The trooper is accelerated by gravity, but
decelerated by drag on the parachute
This problem is from Cleve Moler’s book
called Numerical Computing with Matlab
(my favorite Matlab book)
Governing Equation
dV 4
m mg V * V
dt 15
Solving ODE’s Numerically
Euler’s method is the simplest approach
Consider most general first order ODE:
dy/dt=f(t,y)
Approximate derivative as (yi+1-yi)/dt
Then:
dy yi 1 yi
f (t , yi )
dt t
yi 1 yi t f (t , yi )
A Problem
Unfortunately, Euler’s method is too good
to be true
It is unstable, regardless of the time step
chosen
We must choose a better approach
The most common is 4th order Runge-
Kutta
Runge-Kutta Techniques
Runge-Kutta uses
a similar, but more
complicated k1 t * f (t , yi )
stepping algorithm t k1
k 2 t * f (t , yi )
2 2
t k2
k3 t * f (t , yi )
2 2
k 4 t * f (t t , yi k3 )
k1 2(k 2 k3 ) k 4
yi 1 yi
6
Approach
Choose a time step
Set the initial condition
Run a series of steps
Adjust time step
Continue
Preparing to Solve Numerically
First, we put the equation in the form
dy
f (t , y )
dt
For our example, the equation becomes:
dV 4 V *V
g
dt 15 m
Solving Numerically
There are a variety of ODE solvers in
Matlab
We will use the most common: ode45
We must provide:
◦ a function that defines the function derived
on previous slide
◦ Initial value for V
◦ Time range over which solution should be
sought
How ode45 works
ode45 takes two steps, one with a
different error order than the other
Then it compares results
If they are different, time step is reduced
and process is repeated
Otherwise, time step is increased
The Solution
clear all
timerange=[0 15]; %seconds
initialvelocity=0; %meters/second
[t,y]=ode45(@f,timerange, initialvelocity)
plot(t,y)
ylabel('velocity (m/s)')
xlabel('time(s)')
The Function
function rk=f(t,y)
mass=80;
g=9.81;
rk=-g-4/15*y.*abs(y)/mass;
My Solution
0
-10
-20
v (m/s)
-30
-40
-50
-60
0 5 10 15
time(s)
Practice
Download the file odeexample.m
Run it to reproduce my result
Run again out to t=30 seconds
Run again for an initial velocity of 10
meters/second
Change to k=0 and run again (gravity
only)
Practice
The outbreak of an insect population can
be modeled with the equation below.
R=growth rate
C=carrying capacity
N=# of insects
Nc=critical population
Second term is due to bird predation
2
dN N rN
RN 1 2
C Nc N
2
dt
Parameters
0<t<50 days What is steady
R=0.55 /day state population?
N(0)=10,000 How long does it
C=10,000 take to get there?
Nc=10,000
2
dN N rN
r=10,000 /day
RN 1 2
C Nc N
2
dt
dh 0.0334 h
dt 10h h 2
4
y g y y
15m
y (0) h
y (0) 0
Preparing for Solution
We must break second order equation
into set of first order equations
We do this by introducing new variable
(z=dy/dt)
z y 4
z z z g
z y 15m
4 y z
z z z g
15m y (0) h; z (0) 0
Solving
Now we have to send a set of equations
and a set of initial values to the ode45
routine
We do this via vectors
Let w be vector of solutions: w(1)=y and
w(2)=z
Let r be vector of equations: r(1)=dy/dt
and r(2)=dz/dt
Function to Define Equation
dy
z w(2)
dt
dz 4
w(2) * w(2) g
dt 15m
function r=rkfalling(t,w)
...
r=zeros(2,1);
r(1)=w(2);
r(2)= -k*w(2).*abs(w(2))-g;
The Routines
tr=[0 15]; %seconds
initv=[600 0]; %start 600 m high
[t,y]=ode45(@rkfalling, tr, initv)
plot(t,y(:,1))
ylabel('x (m)')
xlabel('time(s)')
figure
plot(t,y(:,2))
ylabel('velocity (m/s)')
xlabel('time(s)')
Function
function r=rkfalling(t,w)
mass=80;
k=4/15/mass;
g=9.81;
r=zeros(2,1);
r(1)=w(2);
r(2)= -k*w(2).*abs(w(2))-g;
General Second Order Equations
d
2
g
2
sin( )
dt r
Systems
For systems of first order ODEs, just
define both equations.
Practice
Consider an ecosystem of rabbits r and
foxes f. Rabbits are fox food.
Start with 300 rabbits and 150 foxes
=0.01
r=w(1)
dr f=w(2)
2r rf
dt function z=rkfox(t,w)
df
f rf alpha=0.01;
dt r=zeros(2,1);
z(1)=2*w(1)-alpha*w(1)*w(2);
z(2)= -w(2)+alpha*w(1)*w(2);
Approach
Start with ode2ndOrder.m
Modify with function from previous slide
Put in time range (0<t<15) and initial
conditions
Higher Order Equations
x k xV
y k y V g
V x y
2
2
Now we need 4 1st order ODEs
x s
s k sV
y z
z k zV g
V s z2 2
The Code
clear all;
tspan=[0 1.1]
wnot(1)=0; wnot(2)=10;
wnot(3)=0; wnot(4)=10;
[t,y]=ode45('rkprojectile',tspan,wnot);
plot(t,y(:,1),t,y(:,3))
figure
plot(y(:,1),y(:,3))
The Function
function r=rkprojectile(t,w)
g=9.81;
x=w(1); s=w(2); y=w(3); z=w(4);
vel=sqrt(s.^2+z.^2);
r=zeros(4,1);
r(1)=s;
r(2)=-s*vel;
r(3)=z;
r(4)=-z*vel-g;
Questions