필터 지우기
필터 지우기

concatenation of solutions of ODE

조회 수: 3 (최근 30일)
Temesgen
Temesgen 2023년 8월 6일
댓글: Star Strider 2023년 8월 7일
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

답변 (1개)

Star Strider
Star Strider 2023년 8월 6일
편집: Star Strider 2023년 8월 7일
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
Temesgen
Temesgen 2023년 8월 7일
Thank you so much!!!
Star Strider
Star Strider 2023년 8월 7일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by