【发布时间】:2020-07-27 20:55:22
【问题描述】:
您好,我被要求在 MATLAB 中使用 fsolve 命令求解 SIR 模型,并且 Euler 3 指向倒退。我真的很困惑如何继续,请帮助。这就是我到目前为止所拥有的。我为 3BDF 方案创建了一个函数,但我不确定如何继续使用 fsolve 并求解非线性 ODE 系统。 SIR模型表示为,3BDF方案表示为
clc
clear all
gamma=1/7;
beta=1/3;
ode1= @(R,S,I) -(beta*I*S)/(S+I+R);
ode2= @(R,S,I) (beta*I*S)/(S+I+R)-I*gamma;
ode3= @(I) gamma*I;
f(t,[S,I,R]) = [-(beta*I*S)/(S+I+R); (beta*I*S)/(S+I+R)-I*gamma; gamma*I];
R0=0;
I0=10;
S0=8e6;
odes={ode1;ode2;ode3}
fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0)
function [xs,yb] = ThreePointBDF(f,x0, xmax, h, y0)
% This function should return the numerical solution of y at x = xmax.
% (It should not return the entire time history of y.)
% TO BE COMPLETED
xs=x0:h:xmax;
y=zeros(1,length(xs));
y(1)=y0;
yb(1)=y0+f(x0,y0)*h;
for i=1:length(xs)-1
R =R0;
y1(i+1,:) = fsolve(@(u) u-2*h/3*f(t(i+1),u) - R, y1(i-1,:)+2*h*F(i,:))
S = S0;
y2(i+1,:) = fsolve(@(u) u-2*h/3*f(t(i+1),u) - S, y2(i-1,:)+2*h*F(i,:))
I= I0;
y3(i+1,:) = fsolve(@(u) u-2*h/3*f(t(i+1),u) - I, y3(i-1,:)+2*h*F(i,:))
end
end
【问题讨论】:
-
请注意,这是 2 阶 BDF 公式。请参阅math.stackexchange.com/q/3051078/115115 了解使用 fsolve 实现隐式方法。
-
这里是another example 使用 fsolve 实现隐式方法。请注意,通常使用 fsolve 来计算隐式步骤是浪费的,尤其是在不重用近似雅可比行列式的情况下。
标签: matlab numerical-methods ode differential-equations