Solve System of ODEs with Multiple values of a parameter using vectorization but not looping.

1 回表示 (過去 30 日間)
At present I am having a code for plotting solutions of a ode system with multiple initial conditions using vectorization. I want to get the solutions of the same system with a single initial conditon but with multiple values of a parameter (say beta=[0.01;0.02] in code given below). I know how to do it with using for loop but I want to use vectorization instead of looping.
This first code is for multiple initial coditions which run properly.
clc;
clear all;
y0 = 10:200:400
n = length(y0);
p0_all = [50*ones(n,1) y0(:)]';
[t,p] = ode45(@(t,p) lotkasystem(t,p,n),0:.1:15,p0_all);
p = reshape(p,[],n);
nt = length(t);
for k = 1:n
plot(p(1:nt,k),p(nt+1:2*nt,k))
hold on
end
title('Predator/Prey Populations Over Time')
xlabel('t')
ylabel('Population')
hold off
function dpdt = lotkasystem(t,p,n)
%LOTKA Lotka-Volterra predator-prey model for system of inputs p.
delta = 0.02;
beta = 0.01;
% Change the size of p to be: Number of equations-by-number of initial
% conditions.
p = reshape(p,[],n);
% Write equations in vectorized form.
dpdt = [p(1,:) .* (1 - beta*p(2,:));
p(2,:) .* (-1 + delta*p(1,:))];
% Linearize output.
dpdt = dpdt(:);
end
This second code is for single initial condition and multiple values of beta which is giving error. Please help to rslove this.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
y0 = 10
beta=[0.01; 0.02]';
n = length(beta);
[t,p] = ode45(@(t,p) lotkasystem(t,p,n),0:.1:15,[10 10]);
p = reshape(p,[],n);
nt = length(t);
for k = 1:n
plot(p(1:nt,k),p(nt+1:2*nt,k))
hold on
end
title('Predator/Prey Populations Over Time')
xlabel('t')
ylabel('Population')
hold off
function dpdt = lotkasystem(t,p,n)
%LOTKA Lotka-Volterra predator-prey model for system of inputs p.
delta = 0.02;
beta=[0.0; 0.02]'
% Change the size of p to be: Number of equations-by-number of initial
% conditions.
p = reshape(p,[],n);
% Write equations in vectorized form.
dpdt = [p(1,:) .* (1 - beta*p(2,:));
p(2,:) .* (-1 + delta*p(1,:))];
% Linearize output.
dpdt = dpdt(:);
end

採用された回答

VBBV
VBBV 2022 年 7 月 30 日
編集済み: VBBV 2022 年 7 月 30 日
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
y0 = 10:200:400
y0 = 1×2
10 210
n = length(y0);
p0_all = [50*ones(n,1) y0(:)]';
beta=[0.0; 0.02]';
n = length(beta);
[t,p] = ode45(@(t,p) lotkasystem(t,p,n),0:.1:15,p0_all);
p = reshape(p,[],n);
nt = length(t);
for k = 1:n
plot(p(1:nt,k),p(nt+1:2*nt,k))
hold on
end
title('Predator/Prey Populations Over Time')
xlabel('t')
ylabel('Population')
hold off
function dpdt = lotkasystem(t,p,n)
%LOTKA Lotka-Volterra predator-prey model for system of inputs p.
delta = 0.02;
beta=[0.01; 0.02]'; % change 0.0 with small value as defined before.
% Change the size of p to be: Number of equations-by-number of initial
% conditions.
p = reshape(p,[],n);
% Write equations in vectorized form.
dpdt = [p(1,:) .* (1 - beta.*p(2,:)); %
p(2,:) .* (-1 + delta*p(1,:))];
% Linearize output.
dpdt = dpdt(:);
end
  4 件のコメント
VBBV
VBBV 2022 年 7 月 30 日
編集済み: VBBV 2022 年 7 月 30 日
this is with single value of beta and you can see the difference in intial conditions too
SAJAN Phutela
SAJAN Phutela 2022 年 7 月 31 日
編集済み: SAJAN Phutela 2022 年 7 月 31 日
I edited the question by taking non-zero beta. Thanks a lot for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by