Effacer les filtres
Effacer les filtres

Using 'fzero' in a for loop

3 vues (au cours des 30 derniers jours)
Jon Stapchuck
Jon Stapchuck le 26 Avr 2021
Commenté : Jon Stapchuck le 26 Avr 2021
Hello,
I am trying to solve for the roots within a 'for loop', and within the function for which I am root finding, I am using a previously defined variable.
It might make it easier to show my code:
A_Astar_x = zeros(1,IL-1);
Mach_Test = zeros(1,IL-1);
M_x = zeros(1,IL-1);
for i=1:IL-1
A_Astar_x(i) = (y(i,JL)-y(i,1))/(y(1,JL)-y(1,1))*A_A;
Mach_Test(i) = @(set) A_Astar_x(i)-(1/set)*((1+.5*(gamma-1)*set^2)/(.5*(gamma+1))).^alpha;
M_x(i) = fzero(Mach_Test(i),.2);
end
I get an error message that says:
Conversion to double from function_handle is not possible.
Does this error message mean that I cannot solve for the roots iteratively like this?
If not, does anyone know any other methods to go about this?
Thanks,
Jon

Réponse acceptée

Matt J
Matt J le 26 Avr 2021
Modifié(e) : Matt J le 26 Avr 2021
Do you really need to save the sequences of anything besides the M_x(i)? If not, then,
for i=1:IL-1
A_Astar_x = (y(i,JL)-y(i,1))/(y(1,JL)-y(1,1))*A_A;
Mach_Test = @(set) A_Astar_x-(1/set)*((1+.5*(gamma-1)*set^2)/(.5*(gamma+1))).^alpha;
M_x(i) = fzero(Mach_Test,.2);
end
  1 commentaire
Jon Stapchuck
Jon Stapchuck le 26 Avr 2021
That solved it, thank you.
I didn't realize you didn't have to save everything unless you plan to actually use it, but that makes a lot of sense. Probably is more efficient as well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by