Fzero using for loop

3 vues (au cours des 30 derniers jours)
Jose Grimaldo
Jose Grimaldo le 9 Avr 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

Réponse acceptée

Stephen23
Stephen23 le 10 Avr 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

Plus de réponses (1)

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

Catégories

En savoir plus sur Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by