why doesn't this ODE45 function work?

This is from the actual tutorial: https://www.youtube.com/watch?v=9nnaFaDlmuc&feature=youtu.be
but when I plug the following commands in :
tSpan=[0;10]; x0=[4999;1;0]; [tSol,xSol] =ode45(@SIRmodel,tSpan, x0) function dxdt=SIRmodel(t,x) dxdt=[-2*x(1)*x(2)/5000; 2*x(1)*x(2)/5000-2*x(2)/3;2*x(2)/3]; end
I get this error message: Undefined function or variable 'SIRmodel'.
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
>> Was there a mistake in the video?

 采纳的回答

The easy solution is to create ‘SIRmodel’ as an anonymous function.
This works:
SIRmodel = @(t,x) [-2*x(1)*x(2)/5000; 2*x(1)*x(2)/5000-2*x(2)/3;2*x(2)/3];
tSpan=[0;10];
x0=[4999;1;0];
[tSol,xSol] = ode45(SIRmodel,tSpan, x0);
figure(1)
plot(tSol, xSol)
grid
To use it as you did, you need to save it as its own separate ‘.m’ file as SIRmodel.m. (If you have R2017b or later, saving it at the end of a script file would probably work.) See the documentation on Function Basics (link) for details, as well as the documentation on Anonymous Functions (link).

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by