Solve System of ODEs with Multiple values of a parameter using vectorization but not looping.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
VBBV
il 30 Lug 2022
Modificato: VBBV
il 30 Lug 2022
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
y0 = 10:200:400
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!