I keep getting the error "Undefined function or variable". What is wrong with this ODE example?

% SIR.m % % Imlements a SIR infection model % dS/dt = -beta SI % dI/dt = beta SI - delta I % dR/dt = delta I % % Inputs: % t - Time variable: not used here because our equation % is independent of time, or 'autonomous'. % x - Independent variable: this contains three % populations (S, I, and R) % Output: % dx - First derivative: the rate of change of the populations
function dx = SIR(t,x)
dx = [0;0;0];
beta = 0.0003;
delta = 1;
dx(1) = -beta * x(1) * x(2); dx(2) = beta * x(1) * x(2) - delta * x(2); dx(3) = delta *x(2);
options = odeset('RelTol', 1e-4, 'NonNegative', [1 2 3]);
[t,x] = ode('SIR', [0 10], [1000 1 0], options);
plot(t,x);
legend('S','I','R')

2 个评论

Please post the complete error message: You did not tell us, which function or variable is unknown.
You can use the "{} Code" button for format a selected text such, that it is readable code in the forum.

请先登录,再进行评论。

 采纳的回答

Perhaps the problem is
[t,x] = ode('SIR', [0 10], [1000 1 0], options);
plot(f,x);
% ^
Do you mean:
plot(t,x)
?
Or perhaps "ode" is unknown, because you mean "ode45"?
Do not use a string to provide the function to be integrated. It works, but is kept as backward compatibility to Matlab before v6.5 only. Prefer a function handle:
[t,x] = ode(@SIR, [0 10], [1000 1 0], options);
I assume the code you have posted consists of 2 parts, or do the call to "ode" (or what ever is meant) really occur inside the function to be integrated?
Another problem: There is a missing operator in
dx(1) = -beta x(1) * x(2);
% ^
Do you mean:
dx(1) = -beta * x(1) * x(2);

3 个评论

I'm sorry, I see that I was to quick on posting the question. I've done som editing on the question now. I copied the example from another page, but it may be too long ago for the new versions of Matlab? The unknown variable is t, but with some changes, it becomes x. And when i type the complete script directly into the command window, I get this error message:
function dx = SIR(t,x)
Error: Function definitions are not permitted in this context.
same goes when I change it to [t,x] = ode45(@SIR, [0 10], [1000 1 0], options);
The error message is clear: You cannot define function in the command window. Then must appear inside an M-file and saved to disk. Insert the code inside the editor instead of using the command window. Note that defining functions is even not allowed inside scripts (M-files which do not start with "function") except for the newest Matlab version.
To abbreviate the discussion: Insert this in the editor:
function yourMainFcn
options = odeset('RelTol', 1e-4, 'NonNegative', [1, 2, 3]);
[t, x] = ode45(@SIR, [0, 10], [1000, 1, 0], options);
plot(t,x);
legend('S','I','R')
end
function dx = SIR(t,x)
dx = [0;0;0];
beta = 0.0003;
delta = 1;
dx(1) = -beta * x(1) * x(2);
dx(2) = beta * x(1) * x(2) - delta * x(2);
dx(3) = delta * x(2);
end
Save it as "yourMainFcn.m" and then you can run it.

请先登录,再进行评论。

更多回答(1 个)

Try
function main
options = odeset('RelTol', 1e-4, 'NonNegative', [1 2 3]);
[t,x] = ode45(@SIR, [0 10], [1000; 1; 0], options);
plot(t,x);
legend('S','I','R')
function dx = SIR(t,x)
dx = [0;0;0];
beta = 0.0003;
delta = 1;
dx(1) = -beta * x(1) * x(2);
dx(2) = beta * x(1) * x(2) - delta * x(2);
dx(3) = delta *x(2);
Best wishes
Torsten.

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by