1180:Lab2 January 15th, 2012

advertisement
1180:Lab2
January 15th, 2012
Goals For This Week
Last semester,we used Euler’s method to solve differential equations.
R actually has it’s own built-in functions. We’ll use the ode function to
look at some simple population growth models with periodic parameters.
1
The ode function
In general, a one dimensional ODE takes the form.
dy
= f (t, y, p)
dt
The derivative of y may be dependent on y itself, the current time t and possibly
another parameter p. As a first example we’ll consider the exponential growth
of a population. We’ll let y stand for the current population size. We’ll let the
exponential growth rate vary with time according to 1 + sin(t) and let the death
rate be the parameter, p.
dy
= (1 + sin(t)) ∗ y − p ∗ y
dt
We have to write the right hand side as a function in R and it must be exactly
like this:
f<-function(t,y,p){
list((1+sin(t))*y-p*y)
}
First, the order of the input arguments is imporant. The independent variable
(in this case t) comes first, then the dependent variable (y) then the parameter
(p). Second, the output of the function must be in a list. The reason for both
of these is that R expects the input and output in certain forms when it solve
the ode for you.
Next we define the list of times at which we want to solve for y(t), the initial
value y(0).
tlist<-seq(0,10)
yini<-1000
1
We can now finally run the ODE. Here I have set the parameter p = 1. We
give the value of the parameter, the initial conditions, the start and end times
and the function to R, then R does the rest. This is why our ‘f’ has to be in
such a particular form.
result<-ode(y=yini,times=tlist,func=f,parms=1)
We can call the output of the ‘ode’ command anything, so I’m going to choose
result. Type result into and you should see the following.
>result
time
1
0
2
1
3
2
4
3
5
4
6
5
7
6
8
7
9
8
10
9
11
10
1
1000.000
1583.601
4121.203
7315.501
5225.973
2046.916
1040.631
1279.020
3143.999
6760.724
6290.671
The first column just numbers your data points 1-11, the second is the corresponding value of t and the third are the values y(t). The output, result, is an
R object called a data frame. We saw these last semester when doing statistical
test. Let’s see what happens when we try to plot the data frame.
>plot(result)
You should have gotten a rather jagged looking line on a plot title ‘1’. The ‘1’
matches the title of the second column in result. We can fix the labels in the
normal manner. We’ll also change the lines to points so it looks better.
>plot(result,ylab=‘population’,main=‘population vs time’,type=’p’)
To fix the jagged line we need to go back and rerun ode with more time points.
We can just change the tlist and ode commands.
tlist<-seq(0,10,.01)
result2<-ode(y=yini,times=tlist,func=f,parms=1)
The head command is useful if you want to take a peek at some data without
actually printing out the whole thing.
head(result2)
time
1
[1,] 0.00 1000.000
[2,] 0.01 1000.050
2
[3,]
[4,]
[5,]
[6,]
0.02
0.03
0.04
0.05
1000.200
1000.450
1000.800
1001.251
Let’s plot the new result, this time in a different color.
lines(result2,col=’red’)
Save this Graph Note that the two results actually agree with each other,
the second one just uses more points. Hopefully this makes it clear, that the
ode command is much more sophisticated than something we could reasonably
write from scratch in one class period...and that it’s pretty sweet.
2
Tasks for this week
1. Include the comparison graph from the previous section. From this point
forward, all graphs should have enough points so that they look smooth.
In addition, all graphs should have lines connecting their points.
2. Solve the ode with p = .9, 1, 1.1. Then plot the results on a single graph
(make the curves different colours).
2
1000∗sin(t) x
3. The equation x′ = x ∗ (1 − x/1000) − p 1000
sin(t)2 +x represents a species
x suffering from periodic predation. In the absence of predation, the
species will reach a population size of 1000. The number of predators
varies periodically according to 1000 sin(t)2 . The number p represents the
strength of predation.
(a) Let p = 1. Solve the ode between t = 0 and t = 10. Use initial
conditions x(0) = 100, 500 and 1000. Plot all three results on the
same graph (use different colours).
(b) Let p = 1.3 and repeat the previous step. This time plot the results
from t = 0 to t = 10.
(c) Let p = 1.5 and repeat the previous step.
(d) Describe what happens when the predation strength gets bigger. Under what circumstances does the population survive under each scenario?
3
Download