SIR病毒模型动态模拟及理论曲线

SIR模型

\( \frac{\mathrm{d}}{\mathrm{d}t}I=r\beta I\frac{S}{N}-\gamma I \\ \frac{\mathrm{d}}{\mathrm{d}t}S=-r\beta I\frac{S}{N} \\ \frac{\mathrm{d}}{\mathrm{d}t}R=\gamma I\)

查看所有模型及说明:点击病毒传染的动态扩散模拟及数学模型

clear
figure
N = 300;        %人口总数
I = 3;          %传染者state==1
S = N - I;      %易感者state==0
R = 0;          %康复者state==2
 
r = 2;          %感染者接触易感者的人数
B = 0.05;       %传染概率
y = 0.03;       %康复概率
days = 500;
Dynamic = false;%设置为true显示动态模拟过程

Sdata=[];

state=zeros(1,N);
index=randperm(N,I);
state(index)=1;
if Dynamic
    axis([0 N 0 N]);
    hold on
end
for i=1:days
    xpos=randperm(N);
    ypos=randperm(N);
    for j=1:N
        if state(j)~=1
            continue
        end
        if rand()<=y
            state(j)=2;
            continue
        end
        dis=sqrt((xpos-xpos(j)).^2+(ypos-ypos(j)).^2);
        l=0;
        for k=1:N
            if state(k)==1 || state(k)==2
                continue
            end
            l=l+1;
            peo(l).dis=dis(k);
            peo(l).num=k;
        end
        T = struct2table(peo);
        sortedT = sortrows(T,'dis');
        sortedS = table2struct(sortedT);
        tp=sortedT{1:r,2};
        for k=1:length(tp)
            if rand()<=B
                state(tp(k))=1;
            end
        end
    end
    Idata(i)=length(find(state==1));
    Sdata(i)=length(find(state==0));
    Rdata(i)=length(find(state==2));
    %a=-8;
    %b=8;
    %xpos=xpos+ a + (b-a).*rand(1,N);
    %ypos=ypos+ a + (b-a).*rand(1,N);
    if Dynamic && rem(i,10)==0
        phd=scatter(xpos,ypos,[],state,"filled");
        title(['第' num2str(i) '天'])
        drawnow
        delete(phd)
    end
end

figure
set(gcf,'visible',true)
days=length(Sdata);
plot(1:days,Sdata,1:days,Idata,1:days,Rdata)
hold on
T = 1:days+100;
for idx = 1:length(T)-1
    if S(idx)<0
        S(idx)=0;
    elseif S(idx)>N
        S(idx)=N;
    end
    if I(idx)<0
        I(idx)=0;
    elseif I(idx)>N
        I(idx)=N;
    end
    if R(idx)<0
        R(idx)=0;
    elseif R(idx)>N
        R(idx)=N;
    end
    S(idx+1) = S(idx) - r*B*S(idx)*I(idx)/N;
    I(idx+1) = I(idx) + r*B*S(idx)*I(idx)/N - y*I(idx);
    R(idx+1) = R(idx) + y*I(idx);
end

plot(T,S,T,I,T,R);
legend('模拟易感者','模拟传染者','模拟康复者','易感者','传染者','康复者')
xlabel('天');ylabel('人数')
title('SIR模型')Code language: Matlab (matlab)

模拟最终截图如下(动态过程未给出):

留下评论