MATLAB Answers

How can I plot a curve with different ranges for x?

조회 수: 6(최근 30일)
Sze Wing HO
Sze Wing HO 2021년 8월 24일
답변: Mathieu NOE 2021년 8월 25일
I would like to plot three ranges for x with three different functions in a same graph in matlab. The first range is 1<x<20. The second range is from 20<x<30 and the last range is 30<x<200. The y functions are Euler method but using different parameters. I have already typed the codes of Euler method but I don't know how to combine the graphs with different ranges for x.
  댓글 수: 10
Mathieu NOE
Mathieu NOE 2021년 8월 25일
If you agree , I will post again the updated code in the answer section, so you can "accept" it if you're ok for it ?

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

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 8월 25일
So basically , the updated code as guessed from your initial request
clear all; clc;
%% r = 3
tau_E = 3.69; % average time in the exposed state
gamma_1 = 1/tau_E;
tau_I = 3.48; % average time in the infected state
gamma_2 = 1/tau_I;
E0 = 10; I0 = 0; N0 = 6e7;
r3 = 3;p = 0.01;
f = @(u3, beta, gamma_1, gamma_2)...
[-beta*u3(1)*u3(3),...
beta*u3(1)*u3(3) - gamma_1*u3(2),...
gamma_1*u3(2) - gamma_2*u3(3),...
(1-p)*gamma_2*u3(3)];
dt = 0.01;
Days = 20; %% time duration : day 1 to 20
t3 = [1:1:Days]; %% time duration : day 1 to 20
u3 = zeros(Days,4); %% cor MN
u3(1,:) = [N0, E0, 0, 0];
N = N0; beta3 = r3*gamma_2/N;
for day = 1:Days-1 %% cor MN
u3_old = u3(day,:);
for k = 1:100
u3_new = u3_old + dt*f(u3_old, beta3, gamma_1, gamma_2);
u3_old = u3_new;
end
u3(day+1,:) = u3_old(:);
end
%% r = 1
tau_E = 3.69; % average time in the exposed state
gamma_1 = 1/tau_E;
tau_I = 3.48; % average time in the infected state
gamma_2 = 1/tau_I;
E0 = 10; I0 = 0; N0 = 6e7;
r1 = 1;p = 0.01;
f = @(u1, beta, gamma_1, gamma_2)...
[-beta*u1(1)*u1(3),...
beta*u1(1)*u1(3) - gamma_1*u1(2),...
gamma_1*u1(2) - gamma_2*u1(3),...
(1-p)*gamma_2*u1(3)];
dt = 0.01;
Days = 11; % time duration : day 20 to 30
% t1 = [20:1:Days]; %t = t';
t1 = [t3(end):1:t3(end)+Days-1]; % time duration : day 20 to 30
u1 = zeros(Days,4); %% cor MN
% u1(1,:) = [N0, E0, 0, 0];
u1(1,:) = u3(end,:); %% cor MN : use last run last sample as new initial conditions
N = N0; beta = r1*gamma_2/N;
for day = 1:Days-1 %% cor MN
u1_old = u1(day,:);
for k = 1:100
u1_new = u1_old + dt*f(u1_old, beta, gamma_1, gamma_2);
u1_old = u1_new;
end
u1(day+1,:) = u1_old(:);
end
%% r = 0.8
tau_E = 3.69; % average time in the exposed state
gamma_1 = 1/tau_E;
tau_I = 3.48; % average time in the infected state
gamma_2 = 1/tau_I;
E0 = 10; I0 = 0; N0 = 6e7;
r2 = 0.8;p = 0.01;
f = @(u2, beta, gamma_1, gamma_2)...
[-beta*u2(1)*u2(3),...
beta*u2(1)*u2(3) - gamma_1*u2(2),...
gamma_1*u2(2) - gamma_2*u2(3),...
(1-p)*gamma_2*u2(3)];
dt = 0.01;
Days = 171; % time duration : day 30 to 200
% t2 = [30:1:Days]; %t = t';
t2 = [t1(end):1:t1(end)+Days-1]; % time duration : day 30 to 200
u2 = zeros(Days,4); %% cor MN
% u2(1,:) = [N0, E0, 0, 0];
u2(1,:) = u1(end,:); %% cor MN : use last run last sample as new initial conditions
N = N0; beta2 = r2*gamma_2/N;
for day = 1:Days-1 %% cor MN
u2_old = u2(day,:);
for k = 1:100
u2_new = u2_old + dt*f(u2_old, beta2, gamma_1, gamma_2);
u2_old = u2_new;
end
u2(day+1,:) = u2_old(:);
end
figure(4);
hold on
plot(t3,u3(:,3))
plot(t1,u1(:,3))
plot(t2,u2(:,3))
title('R = 3 / 1 / 0.8');
xlabel('days');
% legend('u3(:,3)','u1(:,3)','u2(:,3)');
legend('R = 3','R = 1','R = 0.8');
hold off
%

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by