【数学建模】传染病SIR模型

SIR模型

经典的SIR模型是一种发明于上个世纪早期的经典传染病模型,此模型能够较为粗略地展示出一种传染病的发病到结束的过程,其核心在于微分方程,其中三个主要量S是易感人群,I是感染人群,R是恢复人群

这三个量都是跟随时间变化的函数,即可以表示为,其中的t我们设定为一个单位时间,我们即有如下的公式:

在这里插入图片描述

然而要列出此种类似的方程我们需要一部分的理想化条件,这些理想化条件是比较重要的,

1.首先即城市的总人数不变,即:

S ( t ) + I ( t ) + R ( t ) = K S(t)+I(t)+R(t)=K S(t)+I(t)+R(t)=K
K为一个常数值,一个恒定量。

2.假设 t 时刻单位时间内,一个病人能传染的易感者数目与此环境内易感者总数s(t)成正比,设定比例系数为β,从而在t时刻单位时间内被所有病人传染的人数为βs(t)i(t)

3.t 时刻,单位时间内从染病者中移出的人数与病人数量成正比,比例系数为γ,单位时间内移出者的数量为γi(t)

故我们可以得知其作用的机制为:易感人数和系数以及感染人数同时作用于总的易感人数,同时恢复人数和恢复系数又对感染人数起到影响。但是同时这又是一个单向性的机制。

基于以上三个条件的假设,我们可以获得其人数变化的机制,也即

1.易感个体的下降率为(注:此处为负数):

在这里插入图片描述

2.感染个体的增长率为:

在这里插入图片描述

3.恢复个体的增长率为:

在这里插入图片描述

我们利用微分方程可以表示如下:(SIR核心的微分方程)

在这里插入图片描述

详细可参考知乎:经典传染病的SIR模型(基于MATLAB)

MATLAB实现

转自:SIR模型实现(matlab)

function dydt = odefun(t,y,A,B)
dydt = zeros(2,1);
dydt(1) = A*y(1)*y(2) - B*y(1);
dydt(2) = -A*y(1)*y(2);
end 
clc
clear
close all;
A = 0.4;
B = 0.1;
I = 0.4;
S = 0.5;

%ode
tspan = [0 50];
y0 = [I S];
[t, y] = ode45(@(t,y)odefun(t,y,A,B), tspan, y0);
r = 1-y(:,1)-y(:,2);

%euler
n = size(r,1);
h = 50 / (n-1);
t_0 = [0:h:50]';
y_i = zeros(n,1);
y_s = zeros(n,1);
y_i(1) = I;
y_s(1) = S;
for i = 1:n-1
    y_i(i+1) = h*[A*y_i(i)*y_s(i) - B*y_i(i)]+y_i(i);
    y_s(i+1) = h*[-A*y_i(i)*y_s(i)]+y_s(i);
end
r_0 = 1 - y_i(:,1) - y_s(:,1);

%画图
subplot(2,2,1);
plot(t,y(:,1),'-o',t,y(:,2),'-.',t,r,'g');
hold on;
legend('生病人数:i(t)','健康人数:s(t)','移除人数:r(t)','Location','Best'); 
ylabel('占人口比例%');
xlabel('时间t');
str = ['接触数λ/μ:',num2str(A/B),' 初始生病人数:',num2str(I),',初始健康人数:',num2str(S)];
text(15,0.4,str,'FontSize',10);
title('SIR模型(ode)');


subplot(2,2,2);
plot(t_0,y_i,'-o',t_0,y_s,'-.',t_0,r_0,'g');
hold on;
legend('生病人数:i(t)','健康人数:s(t)','移除人数:r(t)','Location','Best'); 
ylabel('占人口比例%');
xlabel('时间t');
str = ['接触数λ/μ:',num2str(A/B),' 初始生病人数:',num2str(I),',初始健康人数:',num2str(S)];
text(15,0.4,str,'FontSize',10);
title('SIR模型(euler)');

subplot(2,2,3);
plot(t_0,y_i,'r-',t,y(:,1),'-.');
diff = sum(abs(y_i - y(:,1)));
str1 = ['生病人数对比图i(t),    误差:',num2str(diff)];
title(str1);
legend('euler','ode','Location','Best'); 
ylabel('占人口比例%');
xlabel('时间t');

subplot(2,2,4);
plot(t_0,y_s,'r-',t,y(:,2),'-.');
diff = sum(abs(y_s - y(:,2)));
str1 = ['健康人数对比图s(t),     误差:',num2str(diff)];
title(str1);
legend('euler','ode','Location','Best'); 
ylabel('占人口比例%');
xlabel('时间t');



在这里插入图片描述

python实现

转自:建立传染病SIR模型代码
其中β为感染率,γ为药物有效性,TS和ND分别为时间间隔和结束时间,TN为区域内人口数(单位为百万)

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()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45654306/article/details/108135965