Clear Filters
Clear Filters

Fzero using for loop

7 views (last 30 days)
Jose Grimaldo
Jose Grimaldo on 9 Apr 2020
Answered: Stephen23 on 10 Apr 2020
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

Accepted Answer

Stephen23
Stephen23 on 10 Apr 2020
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

More Answers (1)

darova
darova on 10 Apr 2020
AC = @(A,x) B - 2 * integral(Eq1,5,A) - x * integral(Eq2,5,A);
%%
Z(j) = fzero(@(A)AC(A,x),0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!