concatenation of solutions of ODE
Show older comments
I want to plot the solutions of simple SIR model under two time intervals (tspan1 and tspan2). After geting the solution of the model within this time intervals, I want to concatenate and plote the solution curve. But, it is not runing due to the error ''imensions of arrays being concatenated are not consistent''. I am wondering any one who can help me by fixing this error. This is my matlab code!
Thank you
function SS
tspan1=[0 10];
t2f=20;
tspan2=[10 20];
S0=1000;
I0=1;
R0=0;
Initial0=[S0, I0, R0];
%%% ODE
function dx = tem(t, x)
dx = zeros(3,1);
beta = 0.003;
delta = 1;
dx(1) = -beta * x(1) * x(2);
dx(2) = beta * x(1) * x(2) - delta * x(2);
dx(3) = delta * x(2);
end
[t1,xx1] = ode45(@(t,x)tem(t,x),tspan1, Initial0);
S1=xx1(:,1);
I1=xx1(:,2);
R1=xx1(:,3);
Initial1=[S1(t2f),I1(t2f),R1(t2f)];
[t2,x2] = ode45(@(t,x)tem(t,x),tspan2, Initial1);
S2=x2(:,1);
I2=x2(:,2);
R2=x2(:,3);
T=[t1,t2];
S = [S1; S2];
I = [I1; I2];
R = [R1; R2];
%plot
plot(T,S,'k', 'Linewidth', 1.75);
% plot(t,S,'k',t,I,'r', t,R,'c', 'Linewidth', 1.75);
% xlabel('Time')
% ylabel('Population')
% title('Dynamics of SIR model')
% legend('S', 'I', 'R');
end
Answers (1)
Star Strider
on 6 Aug 2023
Edited: Star Strider
on 7 Aug 2023
I would create one ‘tspan1’ vector with a specific number of elemensts, and the same for ‘tspan2’ although they are only required to have the same numbers of elements if you want to horizontally concatenate them. Since they are column vectors, they can have different numbers of elements if you want to vertically concatenate them. The problem is that unless you specify the elements of the ‘tspan’ argument, the MATLAB ODE integrators will produce time vectors of whatever lengths they believe is appropriate, and in a situation such as this, they might not always be the same.
One approach —
tspan1 = linspace(0, 10, 25); % Use 'linspace' To Generate 'tspan'
t2f=20;
tspan2 = linspace(10, 20, 25);
S0=1000;
I0=1;
R0=0;
Initial0=[S0, I0, R0];
[t1,xx1] = ode45(@(t,x)tem(t,x),tspan1, Initial0);
S1=xx1(:,1);
I1=xx1(:,2);
R1=xx1(:,3);
Initial1=[S1(t2f),I1(t2f),R1(t2f)];
[t2,x2] = ode45(@(t,x)tem(t,x),tspan2, Initial1);
S2=x2(:,1);
I2=x2(:,2);
R2=x2(:,3);
T=[t1; t2];
S = [S1; S2];
I = [I1; I2];
R = [R1; R2];
%plot
figure
plot(T,S,'k', 'Linewidth', 1.75);
hold on
plot(T,I,'c', 'Linewidth', 1.75);
plot(T,R,'m', 'Linewidth', 1.75);
hold off
% plot(t,S,'k',t,I,'r', t,R,'c', 'Linewidth', 1.75);
% xlabel('Time')
% ylabel('Population')
% title('Dynamics of SIR model')
legend('S', 'I', 'R', 'Location','best');
% end
function SS
tspan1=[0 10];
t2f=20;
tspan2=[10 20];
S0=1000;
I0=1;
R0=0;
Initial0=[S0, I0, R0];
end
%%% ODE
function dx = tem(t, x)
dx = zeros(3,1);
beta = 0.003;
delta = 1;
dx(1) = -beta * x(1) * x(2);
dx(2) = beta * x(1) * x(2) - delta * x(2);
dx(3) = delta * x(2);
end
Make appropriate changes to get the result you want.
EDIT — (7 Aug 2023 at 00:24)
Corrected typographical errors, clarified discussion.
.
4 Comments
Temesgen
on 7 Aug 2023
I am not certain what you are actually doing. If your function has parameters that you want to change (your SIR model does not), your current approach is correct in stopping the integration, saving the last integrated values to use as initial conditions for the next integration. The only change you need to do is to change the parameters for your ODE function aftter the end of the first integration (or any subsequent integration if you want to do this more than once).
Sketching that out —
tspan = [0 10];
p = [0.003 1; % Parameter Matrix
0.006 2;
0.009 3];
S0=1000;
I0=1;
R0=0;
Initial0=[S0, I0, R0];
for k = 1:size(p,1)
[t,x] = ode45(@(t,x)tem(t,x,p(k,:)),tspan, Initial0);
tc{k,:}= t; % Store Results As Cell Arrays (For Convenioence)
xc{k,:} = x;
Initial0 = x(end,:);
tspan = tspan+t(end);
end
tv = cell2mat(tc); % Conovert To Numeric Arrays For Plotting
xm = cell2mat(xc);
figure
plot(tv, xm)
grid
xlabel('Time')
ylabel('Population')
title('Dynamics of SIR model')
legend('S', 'I', 'R', 'Location','best');
function dx = tem(t, x, p)
dx = zeros(3,1);
beta = p(1);
delta = p(2);
dx(1) = -beta * x(1) * x(2);
dx(2) = beta * x(1) * x(2) - delta * x(2);
dx(3) = delta * x(2);
end
I am certain you can follow this, however if you have any problems, I will do my best to resolve them.
.
Temesgen
on 7 Aug 2023
Star Strider
on 7 Aug 2023
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
