Undefined function or variable 'S', help!

1 次查看(过去 30 天)
I have this function
function z = funct(t,S)
z = 0.4*S-((0.4*S.^2)./10);
and
function euler(func,S0,dt,t0,tf)
% Time interval
t=t0:dt:tf;
% Loop using Euler's method
for i = 1:length(t)-1
S(i+1) = S(i) + dt*(feval(func,t(i),S(i)));
end
t=t'
S=S'
plot(t,S)
xlabel('Time')
ylabel ('S')
when I put this into the command window:
euler(@funct,7,.001,0,25)
I get this error:
Undefined function or variable 'S'.
Error in euler (line 8)
S(i+1) = S(i) + dt*(feval(func,t(i),S(i)));
  1 个评论
Walter Roberson
Walter Roberson 2016-2-22
Instead of using
feval(func, t(i), S(i))
use
func(t(i), S(i))
unless you specifically want to allow the user to pass a function name as a string instead of as a function handle.

请先登录,再进行评论。

回答(1 个)

jgg
jgg 2016-2-22
You never define S in your function. You probably want something like this:
function euler(func,S0,dt,t0,tf)
% Time interval
t=t0:dt:tf;
S = zeros(1,length(t)-1)
S(1) = S0;
% Loop using Euler's method
for i = 1:length(t)-1
S(i+1) = S(i) + dt*(feval(func,t(i),S(i)));
end
t=t'
S=S'
plot(t,S)
xlabel('Time')
ylabel ('S')

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by