SIR 模型实施(matlab)
最编程
2024-04-02 15:44:53
...
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
推荐阅读
-
数学建模]排队论模型和带图形用户界面的 MATLAB 实现
-
使用 SBM-DEA 模型优化绩效评估:理论、案例和 MATLAB 实践
-
大语言模型]的应用:10 分钟搜索引擎实施
-
v 模型实施原则 - v 模型功能简介
-
MATLAB 调用训练有素的 Alexnet 模型进行分类测试
-
[状态估计]基于 FOMIAUKF、分数阶模块、模型估计和多新兴趣系数的电池 SOC 估计研究(Matlab 代码实现)
-
SIR 模型实施(matlab)
-
Phase_Shift_T:基于 MATLAB/Simulink 的移相变压器仿真模型,用于 -25°、-15° ......25°移相。
-
m 基于 simulink 对 16QAM 和 2DPSK 通信链路进行仿真,通过 matlab 调用 simulink 模型获得误码率曲线
-
(顶刊转载)配电网两阶段鲁棒故障恢复(matlab 实现)-2.1 故障恢复的确定性优化模型