Lotka Volterra with an additional parameter

조회 수: 5 (최근 30일)
Khushi Patel
Khushi Patel 2020년 9월 25일
댓글: Star Strider 2020년 9월 25일
I have attempted to model the equations provided below. the code keeps giving me an error when I try to plot the figure using the parameter k instead of t. I am trying to model the effect of varying k on the population dynamics for both prey and predator.
note: if I use plot(k, P) it gives me a complicated and entirely different graph that is unreadable.
Script Window:
tspan = [0, 400];
P0 = [200, 50];
k = [-1, 1];
[t,P] = ode45(@(t,P)lotkavolterra(t,P),tspan,P0);
plot(k,P(:,1),k,P(:,2)) %This section gives me the error. It works, however, if I use t instead of k.
function [dPdt] = lotkawithk(t,P)
alpha = 1;
beta = 0.05;
gamma = 0.05;
delta = 0.0025;
x = P(1);
y = P(2);
dPdt = zeroes(2,1);
dPdt(1) = alpha*x - (beta*x*y)/((log(exp(1)+t))^k);
dPdt(2) = (delta*x*y)/((log(exp(1)+t))^k) - (gamma*y);
end

채택된 답변

Star Strider
Star Strider 2020년 9월 25일
I cannot imagine how you got that code to work at all, considering that the function you call in ode45 is not the function you posted!
Try this:
tspan = linspace(0, 400, 100);;
P0 = [200, 50];
k = [-1, 1];
for k1 = 1:numel(k)
[t,P] = ode45(@(t,P)lotkawithk(t,P,k(k1)),tspan,P0);
Pm{k1} = P;
end
figure
hold on
for k1 = 1:numel(k)
plot(t,Pm{k1}(:,1), t,Pm{k1}(:,2), 'DisplayName',sprintf('k = %d',k(k1))) %This section gives me the error. It works, however, if I use t instead of k.
end
grid
legend
function [dPdt] = lotkawithk(t,P,k)
alpha = 1;
beta = 0.05;
gamma = 0.05;
delta = 0.0025;
x = P(1);
y = P(2);
dPdt = zeros(2,1);
dPdt(1) = alpha*x - (beta*x*y)/((log(exp(1)+t))^k);
dPdt(2) = (delta*x*y)/((log(exp(1)+t))^k) - (gamma*y);
end
Note that ‘k’ is passed as a parameter.
Make appropriate changes to get different results.
  댓글 수: 6
Khushi Patel
Khushi Patel 2020년 9월 25일
Oh okay. Greatly appreciate the help and the clarification about plotting k!
Star Strider
Star Strider 2020년 9월 25일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Khushi Patel
Khushi Patel 2020년 9월 25일
The equations are:

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by