In this lab we will use simulation to study various difference
equations. There are 4 questions that you should answer before
you leave the lab today.
1. Linear Homogeneous
Solve x(n+1) = a x(n) for various values of a.
Copy the following MATLAB code into a m-file and save it as "diff1.m"
% script file to simulate the difference equation x(n+1) = a x(n)
a = input('Enter the value for a:');
x(1) = input('Enter the initial value (x(0)):');
for n = 1:100
x(n+1) = a*x(n);
end
plot(x)
title(['Difference Equation with a = ',num2str(a)])
% end of script
Run it by typing diff1. It will prompt you
for a value for a and an initial value x(0). Try it
with a = 0.2, 0.5, 0.9, 0.95, 1, 1.05, 1.1, -1, -0.5.
You can use any value you want for x(0).
Question 1 For what range of values for a does the solution
tend toward 0? For what range of values does it tend toward infinity?
2. Linear Non-Homogeneous
Solve x(n+1) = a x(n) + b for various values of a and b.
I put a copy of a MATLAB script below, but you might try to see if
you can figure out how to modify the previous script to solve this
problem. Note: you should have the user input a value for b.
Call this file "diff2.m". You run it by typing diff2
It will prompt you for the values of a, b and x(0).
This difference equation has an equilibrium solution of x(n) = b/(1-a).
Run the script with (a,b) = (1.1,-10), (0.9,10), (1.5,-100), (0.5,100)
For each pair, use initial values near the equilibrium value, for example
if a = 1.2 and b = -20, then the equilibrium value is
-20/(1-1.2) = 100, so try x(0) = 105 and x(0) = 95.
Question 2 For what range of values for a does the solution
tend toward the equilibrium value?
3. Linear Predator-Prey
Let x(n) be the number of rabbits and y(n) be the number
of foxes after n weeks. The model for their interaction is:
x(n+1) = x(n) + 0.1 x(n) - 0.25 y(n)
y(n+1) = y(n) - 0.2 y(n) + 0.15 x(n)
On their own rabbits would grow in number, foxes would decrease.
Write a MATLAB script (or use the one below) to simulate this
system for 100 weeks. Plot the number of foxes vs. the number
of rabbits, i.e. y vs. x.
Question 3 Try starting with 20 rabbits and 4 foxes. Do both
species survive? Try increasing the starting rabbit population. Is
there any value which lets both species survive?
4. Nonlinear Predator-Prey
Let x(n) and y(n) be as in 1. Now we are going to
model the interaction between the rabbits and the foxes by a
mixing term: x(n)y(n). The model is then
x(n+1) = x(n) + 0.1 x(n) - 0.02 x(n)y(n)
y(n+1) = y(n) - 0.2 y(n) + 0.01 x(n)y(n)
On their own rabbits would grow in number, foxes would decrease.
When the number of rabbits is low, it is harder for the foxes to
find them.
Write a MATLAB script (or use the one below) to simulate this
system for 100 weeks. Plot the number of foxes vs. the number
of rabbits, i.e. y vs. x.
Question 4 Try starting with 20 rabbits and 4 foxes. Do both
species survive? What do you think would happen if you extended the
simulation? Try different starting values. Is there an equilibrium
value, i.e. are there starting values so that the populations
never change?
5. Other Interactions (Extra)
Here are some other types of interactions you can study:
Arms Race
x(n+1) = 100 + 0.5 y(n)
y(n+1) = 75 + 0.8 x(n)
Competeting Species
x(n+1) = x(n) + 0.1 x(n) - 0.25 y(n)
y(n+1) = y(n) + 0.05 y(n) - 0.15 x(n)
On their own, each would grow in number, and each are hurt by
the existence of the other.
Nonlinear version:
x(n+1) = x(n) + 0.1 x(n) - 0.02 x(n)y(n)
y(n+1) = y(n) + 0.05 y(n) - 0.01 x(n)y(n)
Symbiotic Relationship
x(n+1) = x(n) - 0.1 x(n) + 0.25 y(n)
y(n+1) = y(n) - 0.05 y(n) + 0.15 x(n)
On their own, each would die, but together they survive.
Sample Scripts
diff2.m
% script file to simulate the difference equation x(n+1) = a x(n) + b
a = input('Enter the value for a:');
b = input('Enter the value for b:');
x(1) = input('Enter the initial value (x(0)):');
for n = 1:100
x(n+1) = a*x(n) + b;
end
plot(x)
title(['Difference Equation with a = ',num2str(a),' and b = ',num2str(b)])
% end of script
predprey.m
% script file to simulate the linear predator-prey model
x(1) = input('Enter the initial number of rabbits:');
y(1) = input('Enter the initial number of foxes:');
for n = 1:100
x(n+1) = x(n) + 0.1*x(n) - 0.25*y(n);
y(n+1) = y(n) - 0.2*y(n) + 0.15*x(n);
end
plot(x,y), xlabel('Rabbits'), ylabel('Foxes')
% end of script
predprey2.m
% script file to simulate the nonlinear predator-prey model
x(1) = input('Enter the initial number of rabbits:');
y(1) = input('Enter the initial number of foxes:');
for n = 1:100
x(n+1) = x(n) + 0.1*x(n) - 0.02*x(n)*y(n);
y(n+1) = y(n) - 0.2*y(n) + 0.01*x(n)*y(n);
end
plot(x,y), xlabel('Rabbits'), ylabel('Foxes')
% end of script
ccollins@math.utk.edu
Last Modified: July 3, 2001