传染病模型matlab代码
时间: 2023-11-16 15:46:40 浏览: 71
matlab传染病模型.doc
以下是基本的传染病模型的MATLAB代码,包括SIR模型和SEIR模型:
SIR模型:
```matlab
% SIR model
function dy = SIR_model(t,y,beta,gamma)
dy = zeros(3,1);
dy(1) = -beta*y(1)*y(2);
dy(2) = beta*y(1)*y(2) - gamma*y(2);
dy(3) = gamma*y(2);
end
% initial conditions
N = 1000; % population size
I0 = 1; % initial number of infected
S0 = N - I0; % initial number of susceptible
R0 = 0; % initial number of recovered
y0 = [S0; I0; R0];
% parameters
beta = 0.3; % infection rate
gamma = 0.1; % recovery rate
% time interval
tspan = [0 100];
% solve ODE
[t,y] = ode45(@(t,y)SIR_model(t,y,beta,gamma),tspan,y0);
% plot results
plot(t,y(:,1),'b',t,y(:,2),'r',t,y(:,3),'g')
legend('Susceptible','Infected','Recovered')
xlabel('Time')
ylabel('Population')
```
SEIR模型:
```matlab
% SEIR model
function dy = SEIR_model(t,y,beta,sigma,gamma)
dy = zeros(4,1);
dy(1) = -beta*y(1)*y(3);
dy(2) = beta*y(1)*y(3) - sigma*y(2);
dy(3) = sigma*y(2) - gamma*y(3);
dy(4) = gamma*y(3);
end
% initial conditions
N = 1000; % population size
E0 = 1; % initial number of exposed
I0 = 0; % initial number of infected
S0 = N - E0 - I0; % initial number of susceptible
R0 = 0; % initial number of recovered
y0 = [S0; E0; I0; R0];
% parameters
beta = 0.3; % infection rate
sigma = 0.1; % incubation rate
gamma = 0.1; % recovery rate
% time interval
tspan = [0 100];
% solve ODE
[t,y] = ode45(@(t,y)SEIR_model(t,y,beta,sigma,gamma),tspan,y0);
% plot results
plot(t,y(:,1),'b',t,y(:,2),'m',t,y(:,3),'r',t,y(:,4),'g')
legend('Susceptible','Exposed','Infected','Recovered')
xlabel('Time')
ylabel('Population')
```
在以上代码中,SIR模型中,S表示易感者(Susceptible)、I表示感染者(Infected)、R表示康复者(Recovered);SEIR模型中,S表示易感者、E表示潜伏者(Exposed)、I表示感染者、R表示康复者。在两个模型中,beta表示感染率,gamma表示康复率。在SEIR模型中,sigma表示潜伏率。
阅读全文