Fzero using for loop
7 次查看(过去 30 天)
显示 更早的评论
I have an anonymous function, im trying to find each fzero value as variable x in the anonymous function (AC) goes from 0:1:10, how can I do that?
Eq1 = @(A) 2A + A^2 - 5A
Eq2 = @(A) 3A + A^3 - 2A
B=5;
AC = @(A) B - 2 * integral(Eq1,5,A) - x * integral(Eq2,5,A);
j=1;
for x=0:1:10
Z(j)=fzero(AC(x),0)
j=j+1
end
0 个评论
采纳的回答
Stephen23
2020-4-10
You need to parameterize the function:
Note that it is usually simpler and more robust to loop over indices rather than looping over data, e.g.:
Eq1 = @(A) 2*A + A.^2 - 5*A;
Eq2 = @(A) 3*A + A.^3 - 2*A;
B = 5;
AC = @(A,x) B - 2*integral(Eq1,5,A) - x*integral(Eq2,5,A);
X = 0:1:10;
N = numel(X);
Z = nan(0,N);
for k = 1:N
Z(k) = fzero(@(A)AC(A,X(k)),0);
end
Giving:
>> Z
Z =
5.2309 5.033 5.0178 5.0122 5.0092 5.0074 5.0062 5.0054 5.0047 5.0042 5.0038
0 个评论
更多回答(1 个)
darova
2020-4-10
AC = @(A,x) B - 2 * integral(Eq1,5,A) - x * integral(Eq2,5,A);
%%
Z(j) = fzero(@(A)AC(A,x),0)
0 个评论
另请参阅
类别
在 Help Center 和 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!