Fzero using for loop

7 次查看(过去 30 天)
Jose Grimaldo
Jose Grimaldo 2020-4-9
回答: Stephen23 2020-4-10
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

采纳的回答

Stephen23
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

更多回答(1 个)

darova
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)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by