建立传染病SIR模型代码

基于SIR模型的传染病模型代码

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

beta = 7e-3
gamma = 4e-3
TS = 1.0
ND = 1000.0
TN = 4.2
S0 = 0.998*TN
I0 = 0.002*TN
INPUT = (S0, I0, 0.0)


def diff_eqs(INP, t):
    Y = np.zeros((3))
    V = INP
    Y[0] = - beta * V[0] * V[1]
    Y[1] = beta * V[0] * V[1] - gamma * V[1]
    Y[2] = gamma * V[1]
    return Y


t_start = 0.0
t_end = ND
t_inc = TS
t_range = np.arange(t_start, t_end + t_inc, t_inc)
RES = spi.odeint(diff_eqs, INPUT, t_range)

print(RES)
with open('SIR.txt', 'w') as f:
    for each in RES:
        f.write(str(each))
        f.write('\n')
f.close()
# Ploting
plt.subplot(111)
plt.plot(RES[:, 1], '-r', label='Infectious')
plt.plot(RES[:, 0], '-g', label='Susceptibles')
plt.plot(RES[:, 2], '-k', label='Recovereds')
plt.legend(loc=0)
plt.title('SIR')
plt.xlabel('Time (day)')
plt.ylabel('Infectious Susceptibles and Recovereds (million)')
plt.show()

其中 β \beta β为感染率, γ \gamma γ为药物有效性,TS和ND分别为时间间隔和结束时间,TN为区域内人口数(单位为百万)。
运行结果为感染人数,疑似人数和治愈人数随时间变化

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 MATLAB 程序,用于模拟基于 SIR 模型传染病传播。SIR 模型将人群分为三个类别:易感者 (Susceptible),感染者 (Infected) 和康复者 (Recovered)。该模型基于以下假设: 1. 人口总数 N 是恒定不变的。 2. 感染者和易感者之间的接触是随机的。 3. 感染者和易感者之间的传染概率是一定的。 4. 康复者具有免疫力,并且不再感染或传播疾病。 在下面的代码中,我们使用欧拉方法来模拟 SIR 模型。该方法利用微分方程的近似值来计算模型的下一个状态。具体而言,该方法在给定时间步长的情况下,根据微分方程的斜率估计每个变量的增量,然后根据该增量更新每个变量的值。代码如下: ```matlab % SIR model simulation for infectious disease spread % Based on Euler method % Author: AI Challenger % Date: 2021-01-05 % Set simulation parameters N = 1000; % Total population beta = 0.3; % Infection rate gamma = 0.1; % Recovery rate steps = 200; % Simulation steps % Initialize variables S = zeros(1, steps); % Susceptible population I = zeros(1, steps); % Infected population R = zeros(1, steps); % Recovered population % Set initial conditions S(1) = N - 1; % One infected individual I(1) = 1; R(1) = 0; % Simulate SIR model for t = 2:steps dS = -beta*S(t - 1)*I(t - 1)/N; % Susceptible individuals become infected dI = beta*S(t - 1)*I(t - 1)/N - gamma*I(t - 1); % Infected individuals recover dR = gamma*I(t - 1); % Recovered individuals gain immunity S(t) = S(t - 1) + dS; I(t) = I(t - 1) + dI; R(t) = R(t - 1) + dR; end % Plot results plot(S, 'b-', 'LineWidth', 2); hold on plot(I, 'r-', 'LineWidth', 2); plot(R, 'g-', 'LineWidth', 2); xlabel('Time (days)'); ylabel('Population'); legend('Susceptible', 'Infected', 'Recovered'); title('SIR Model Simulation'); ``` 请注意,该代码仅是一个简单的示例,可能与实际情况不完全相符。在实际应用中,您需要根据您所研究的具体疾病和人口特征进行相应的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值