0% found this document useful (0 votes)
4 views

Solving Initial Value Problems: Jake Blanchard University of Wisconsin - Madison Spring 2008

The document discusses solving initial value problems for ordinary differential equations numerically. It introduces Euler's method and the Runge-Kutta method. It provides examples of applying these methods to solve problems involving falling objects, populations over time, and more. The document also discusses solving systems of equations and higher order differential equations.

Uploaded by

maxterminator555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Solving Initial Value Problems: Jake Blanchard University of Wisconsin - Madison Spring 2008

The document discusses solving initial value problems for ordinary differential equations numerically. It introduces Euler's method and the Runge-Kutta method. It provides examples of applying these methods to solve problems involving falling objects, populations over time, and more. The document also discusses solving systems of equations and higher order differential equations.

Uploaded by

maxterminator555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Solving Initial Value Problems

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

 m=paratrooper mass (kg)


 g=acceleration of gravity (m/s2)
 V=trooper velocity (m/s)
 Initial velocity is assumed to be zero

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

 Note: this is a first order ode


 Skeleton script is in file: insects.m
Insects.m
function insects
clear all
tr=[0 ??];
initv=??;
[t,y]=ode45(@f, tr, initv);
plot(t,y)
ylabel('Number of Insects')
xlabel('time')
%
function rk=f(t,y)
rk= ??;
Practice
 Let h be the depth of water in a spherical
tank
 If we open a drain at the tank bottom, the
pressure at the bottom will decrease as
the tank empties, so the drain rate
decreases with h
 Find the time to empty the tank
Parameters

 R=5 ft; Initial height=9 ft


 1 inch hole for drain

dh 0.0334 h

dt 10h  h 2

 How long does it take to drain


the tank?
Rockets

 A rocket’s mass decreases as it burns fuel


 Find the final velocity of a rocket if:
 T=48000 N; m0=2200 kg
 R=0.8; g=9.81 m/s2; b=40 s
dv
m  T  mg
dt
 rt 
m  m0 1  
 b
Options
 Options are available to:
◦ Change relative or absolute error tolerances
◦ Maximum number of steps
◦ Etc.
Some Other Matlab routines
 ode23 – like ode45, but lower order
 ode15s – stiff solver
 ode23s – higher order stiff solver
Advanced IVPs
 Second order equations
 Stiff equations
Second Order Equations
 Consider a falling object with drag

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

 We can write a general d2y dy


second order equation 2
 f (t , y, )
dt dt
as shown: or
 To solve:
dy
◦ Define f z
dt
◦ Set initial conditions
dz
◦ Set time range  f (t , y, z )
dt
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)') function r=rkfalling(t,w)
xlabel('time(s)') mass=80;
figure k=4/15/mass;
plot(t,y(:,2)) g=9.81;
ylabel('velocity (m/s)') r=zeros(2,1);
xlabel('time(s)') r(1)=w(2);
r(2)= -k*w(2).*abs(w(2))-g;
Practice
 Return to paratrooper problem.
 Download ode2ndOrder.m
 Run to duplicate earlier results for
velocity
 Change initial velocity to 10 m/s and run
again
2
d y 4 dy dy
m 2  mg 
dt 15 dt dt
Practice-nonlinear pendulum
 r=1 m; g=9.81 m/s2
 Initial angle =/8, /2, -0.1

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

 Suppose we want to model a projectile

x  k xV
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

You might also like