In matlab Loops. In the Lotka Volterra predator-prey model, the changes in the p
ID: 3846998 • Letter: I
Question
In matlab
Loops. In the Lotka Volterra predator-prey model, the changes in the predator population y and the prey population x are described by the following equations:
xt=xt+1xt=axtbxtyt
yt=yt+1yt=cxtytdyt
Write a function simulatepredatorprey(x,y, a,b,c,d, T) that takes in the initial population sizes of x and y and simulates the model with the input parameters, for T discrete time steps (in years), updating the population sizes after each time step. For each time step, calculate the number of predator & prey from the population sizes of the previous year using the difference equations above. Your function should return the history of the population sizes as a 2-row matrix (first row containing the history of x and second row containing the history of y), starting with the initial population sizes in the first column, and with each additional column containing the population sizes of x and y at that time step.
Your function should also plot the population the predator and the prey over time. Predator and prey plots should use different line and marker styles. The axes should be labeled and the figure should contain a legend. Assume the time steps are given in years and the population sizes are given in thousands.
>> disp(simulatepredatorprey(10,20, 0.1, 0.01, 0.01, 0.2, 1))
10 9
20 18
>> disp(simulatepredatorprey(10,20, 0.1, 0.01, 0.01, 0.2, 5))
10.0000 9.0000 8.2800 7.7815 7.4592 7.2791
20.0000 18.0000 16.0200 14.1425 12.4145 10.8576
Explanation / Answer
%Define a function
function pp=simulatepredatorprey(x,y,a,b,c,d,T)
%Get value of N
N=input('Time of the whole simulation (in years) N=');
%Assign value
X(1)=x;
%Assign value
Y(1)=y;
%Assign value
xx=x;
%Assign value
yy=y;
%Assign value
t=T;
%Declare variable
i=1;
%Loop until length
for n=1:1:N
%Assign value
XX(n)=xx;
%Assign value
YY(n)=yy;
%Compute xx
xx=a*XX(n)-b*XX(n)*YY(n)+XX(n);
%Compute yy
yy=c*XX(n)*YY(n)-d*YY(n)+YY(n);
%If n equals t
if n==t
%Update i
i=i+1;
%Assign value
X(i)=xx;
%Assign value
Y(i)=yy;
%Update t
t=t+T;
%End
end
%End
end
%Assign value
pp=[X;Y];
%End
end
%Call method
disp(simulatepredatorprey(10,20,0.1,0.01,0.01,0.2,1))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.