首页 > 其他分享 >SIR模型实现(matlab)

SIR模型实现(matlab)

时间:2022-10-29 10:00:39浏览次数:94  
标签:SIR 模型 -. num2str matlab 人数 Location

 

matlab代码

 

 

 

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');

 

 

 


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

 

结果

 

本文出自:https://www.cnblogs.com/shish/p/12685865.html

 招募大量matlab技术人员,有大量matlab需求订单,均为个人短期可以完成,有时间的朋友可以加我微信 : nn505203 ,有需求也可以联系此微信 。加好友备注博客园matlab技术即可。

 

 

 

 

 

标签:SIR,模型,-.,num2str,matlab,人数,Location
From: https://www.cnblogs.com/LQX989/p/16838107.html

相关文章