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?

 Réponse acceptée

Star Strider
Star Strider le 18 Mar 2018
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).

2 commentaires

Saskya Byerly
Saskya Byerly le 18 Mar 2018
Thank you!
Star Strider
Star Strider le 18 Mar 2018
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by