Undefined function or variable 'S', help!

조회 수: 1 (최근 30일)
Danielle Moore
Danielle Moore 2016년 2월 22일
댓글: Walter Roberson 2016년 2월 22일
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