Borrar filtros
Borrar filtros

concatenation of solutions of ODE

3 visualizaciones (últimos 30 días)
Temesgen
Temesgen el 6 de Ag. de 2023
Comentada: Star Strider el 7 de Ag. de 2023
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

Respuestas (1)

Star Strider
Star Strider el 6 de Ag. de 2023
Editada: Star Strider el 7 de Ag. de 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 comentarios
Temesgen
Temesgen el 7 de Ag. de 2023
Thank you so much!!!
Star Strider
Star Strider el 7 de Ag. de 2023
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by