Docsity

Euler's Method for Solving ODEs: Classical Predator-Prey Model - Prof. Joh Calandrino, Assignments of Computer Science

Toggle sidebar
bg1
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.
pf2

Partial preview of the text

Download Euler's Method for Solving ODEs: Classical Predator-Prey Model - Prof. Joh Calandrino and more Assignments Computer Science in PDF only on Docsity!

(^00 5 10 15 20 ) 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 –rf to the change in the rabbit population and

+rf 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.

  1. 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?
  2. 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?
  3. 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.

The Taylor series gives us mathematical justification for what we are doing, and gives an error bound. If we know a function f and first derivative at 0, and want to evalute f(h), we can write the first order Taylor series, with its error term.

  1. If we have a bound on the second derivative f''(x) < C for 0 ≤ x ≤ h , then as we make h small, the error we make in one step is at most what?
  2. Of course, if we make h smaller, then it takes more steps to get from 0 to t max, so the error can accumulate to what? Does making h smaller make the final answer more accurate? We now have a tradeoff: h large is faster and h small gives a better guarantee on maximum error. What if we could get an idea of the actual error, instead of the maximum? Then whenever we find the error is large, we could switch to taking smaller steps.
  3. Suppose that we compute each step twice: once by taking a step of size h and once by taking two steps of size h /2 each. If the difference between these is large, then we could make h smaller; if it is small, we could make h larger. For experts: write this code, keeping the error between 10-5^ and 10-8. In fact, ode45 does something like this: it uses a 4-step and a 5-step approximation to estimate error at each "step" and makes h smaller or larger to ensure accuracy.