
0 5 10 15 20 25
0
20
40
60
80
100
120
COMP 116
Oct 22, 2007
Euler's method: simple numerical integration for ODEs
MATLAB's ode45 is a sophisticated differential equation solver that is built on some
simple ideas. As programming practice, I'm asking you to develop these ideas in class.
First, we need a differential equation. Here is a classical (Lotka-Volterra) predator/prey
model: Suppose an area has r rabbits and f foxes. Each year, every pair of rabbits has
four babies. If there are no rabbits, then all the foxes starve to death. If each fox has a
chance of eating each rabbit, meaning the rabbit dies and the fox survives for of the
year, then this would contribute –rf to the change in the rabbit population and
+rf to the change in the fox population. What are the equations
for rate of change of rabbits and foxes per year?
1. Complete this function, using and :
function rfprime = rabbitsNfoxes(t, rf)
% model rabbits r = rf(1) and foxes f = rf(2) by
% dr/dt =
% df/dt =
alpha = 0.2;
epsilon = 0.1;
rfprime =
The call ode45(@rabbitsNfoxes, [0,25], [100;10]); generated the plot above, which
oscilates between plenty and starvation over 25 years, starting with 100 rabbits and 10 foxes.
2. Suppose that we just use the derivative at t=0 to estimate what the population is at year
t=1, starting from rf = [100; 10]. What would you write for MATLAB?
3. Suppose that we use the derivative at t=0 to estimate what the population is at year t=1/2,
starting from rf = [100; 10]. What would you write for MATLAB? What is the new
derivative at t=1/2?
4. Suppose that we want to find out the population at step size h for 25 years. (h = 1/12;
would give monthly populations). That is, we want to population at times t = 0:h:25.
Write a for loop that will compute these populations and save them so they can be
plotted.