필터 지우기
필터 지우기

anonymous function does not calculate a numbers

조회 수: 1 (최근 30일)
Abdulaziz
Abdulaziz 2012년 11월 27일
% Hi guys please I need help with an issue I could not figure.
>> % This is simplified example of my issue
>> for i=1:5
y=i*x^2;
dy=diff(y,x);
g=@(x)dy;% this equation is changing in each iteration due to 'i'.
m=g(i)
end
m =2*x
the anonymous function does not account for the i, it always gives the variable x in the m equation.

채택된 답변

Walter Roberson
Walter Roberson 2012년 11월 27일
You are overwriting "m" on each iteration of the "for i" loop.
Should we assume that "x" is defined as a symbol?
Please check that "i" is the variable name being used in your loop. Using "i" as a variable name is not a good idea as it clashes with the use of "i" meaning the square root of negative 1.
Note that the "x" of @(x) will not be the same "x" as in "dy". When a dummy argument name is used in an anonymous function, the dummy argument value will only be substituted for variables with that name that occur literally in the anonymous function body. You could, for example, use
g = @(x) subs(dy, 'x', x)
provided that dy is symbolic.

추가 답변 (1개)

Abdulaziz
Abdulaziz 2012년 11월 27일
Thank you Walter as usually you always give the best easy to understand answers.

Community Treasure Hunt

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

Start Hunting!

Translated by