【问题标题】:Comparing results of "ODE45" function versus 4th order Runge-Kutta for Influenza disease in MATLAB在 MATLAB 中比较“ODE45”函数与 4 阶 Runge-Kutta 对流感疾病的结果
【发布时间】:2021-08-21 06:46:55
【问题描述】:

我为一个非线性微分方程系统编写了一个代码,该系统对流感疾病进行建模。当我想用 4 阶 Runge-Kutta 和 ode45function 模拟这个系统时,我感到非常震惊。问题是这两种方法的结果是不一样的。事实上,当我为 4 阶 Runge-Kutta 算法编写代码时,状态会在每一步中变得越来越大,直到达到无穷大。但是当我用ode45function 模拟系统时,结果是合理合理的。我不认为我写的算法是不正确的。有人可以向我解释我做错了什么吗?如果代码中的某些内容无效?

我写的代码如下:

clc;clear;close all;

t = linspace(0,100,101)';
global alpha eta p f ep delta q beta kessi

kessi = 0.526;                % Transition rate for the exposed
alpha = 0.244;                % Recovery rate for the (symptomatic)
eta = alpha;                  % Infected
p = 0.667;                    % Recovery rate for the asymptomatic
f = 0.98;                     % 1-fatallity rate(one minus fatality rate)
ep = 0;                       % Infectivity reduction factor for the exposed
delta = 1;                    % Infectivity reduction factor for the asymptomatic
q = 0.5;                      % Contact reduction by isolation
beta = 1;                     % From CALCULATIONS

Influenza_Fun = @(t,x,u) [-beta*x(1)*(ep*x(2)+(1-q)*x(3)+delta*x(4))-u(1)*x(1)
                           beta*x(1)*(ep*x(2)+(1-q)*x(3)+delta*x(4))-kessi*x(2)
                            p*kessi*x(2)-alpha*x(3)-u(2)*x(3)
                            (1-p)*kessi*x(2)-eta*x(4)
                            f*alpha*x(3)+u(2)*x(3)+eta*x(4)+u(1)*x(1)];


% Initial Condtions
Ics = [15000 200 500 300 0]';                     % [S0 E0 I0 A0 R0]';
u = [0 0]';

[~,States] = ode45(@(t,x)Influenza_Fun(t,x,u),t,Ics);

for i=1:5
   plot(t,States(:,i),'LineWidth',2)
   hold on
   grid on

end
legend('S','E','I','A','R')
legend show

以上代码将基于ode45function 模拟系统。以下代码是我的 RK4 算法:

clc;clear;close all;

% { Control of Influenza disease based on Genetic Algorithm using desired Objective FUnction}

global alpha eta p f ep delta q beta kessi


kessi = 0.526;                % Transition rate for the exposed
alpha = 0.244;                % Recovery rate for the (symptomatic)
eta = alpha;                  % Infected
p = 0.667;                    % Recovery rate for the asymptomatic
f = 0.98;                     % 1-fatallity rate(one minus fatality rate)
ep = 0;                       % Infectivity reduction factor for the exposed
delta = 1;                    % Infectivity reduction factor for the asymptomatic
q = 0.5;                      % Contact reduction by isolation
beta = 1;                     % From CALCULATIONS

% Initial Condtions
Ics = [15000 200 500 300 0]';                     % [S0 E0 I0 A0 R0]';

max_days = 100;
y(:,1) = Ics;
t = zeros(1,max_days);

h = 1;
t(1) = 0;
LB = [0 0]';
UB = [1 1]';
g = @yMatrix; 
u = [0 0]';

Influenza_Fun = @(t,x,u) [-beta*x(1)*(ep*x(2)+(1-q)*x(3)+delta*x(4))-u(1)*x(1)
                           beta*x(1)*(ep*x(2)+(1-q)*x(3)+delta*x(4))-kessi*x(2)
                            p*kessi*x(2)-alpha*x(3)-u(2)*x(3)
                            (1-p)*kessi*x(2)-eta*x(4)
                            f*alpha*x(3)+u(2)*x(3)+eta*x(4)+u(1)*x(1)];


%% 2. mAiN Loop

for time = 1:max_days


    k1 = Influenza_Fun(t(time),y(:,time),u);
    k2 = Influenza_Fun(t(time)+(h/2),y(:,time)+k1,u);
    k3 = Influenza_Fun(t(time)+(h/2),y(:,time)+k2,u);
    k4 = Influenza_Fun(t(time)+h,y(:,time)+k3,u);

    % y(i+1) = y(i) + (h/6)*(k1+2k2+2k3+k4)
    y(:,time+1) = y(:,time)+(h/6)*(k1+2*k2+2*k3+k4);
    t(time+1) = t(time)+h;

end

y = y';

for i=1:size(y,2)

    plot(t,y(:,i),'LineWidth',2)
    hold on
    grid on


end

【问题讨论】:

    标签: differential-equations runge-kutta ode45


    【解决方案1】:

    应该是

        k2 = Influenza_Fun(t(time)+(h/2),y(:,time)+(h/2)*k1,u);
    

    等等

    还可以使用某种方式来展示 ode45 实际使用的时间步长,例如使用 [ T,Y] = 输出格式,从 Refine 选项中扣除额外的分数。或者使用函数评估计数,至少文档中的最后一个调用变体应该显示这一点。在 RK4 中使用类似的步长或函数调用计数以获得可比较的结果。

    【讨论】:

    • 没错。我们可以更改时间步以获得更好的结果。我要问的是,为什么“ode45”和RK4的结果应该在两者的步长为1时彼此不同?
    • 您需要仔细研究 ode45 的工作原理。时间步肯定不是 1,这只是产生输出的点的时间步。您没有任何迹象表明采取了多少内部步骤。要使用 RK4 进行模拟,您可以使用时间步长 0.1 计算解决方案,并且仅在输出数组中包含每 10 步。
    • 所以你的意思是,例如,当我使用 t = linspace(0,100,101) 步长为 1 时,ode45 将解决这个范围的解决方案,或者它需要任意的步长时间。这个带有步长的时间向量到底会不会包含在求解中?
    • 不,此输出时间数组不会影响内部求解器步骤。在每个内部步骤之后,求解器检查它是否通过了下一个输出时间,并从“密集输出”多项式中插入相应的值。
    • 非常感谢您的解释。那么,如果我想为每种方法获得相同的结果,您建议我怎么做?
    猜你喜欢
    • 2014-03-18
    • 2017-09-21
    • 2023-02-18
    • 2023-03-20
    • 1970-01-01
    • 2015-03-19
    • 2021-10-21
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多