anonymous function does not calculate a numbers
1 回表示 (過去 30 日間)
古いコメントを表示
% 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.
0 件のコメント
採用された回答
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.
0 件のコメント
その他の回答 (1 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!