Azzera filtri
Azzera filtri

Using 'fzero' in a for loop

3 visualizzazioni (ultimi 30 giorni)
Jon Stapchuck
Jon Stapchuck il 26 Apr 2021
Commentato: Jon Stapchuck il 26 Apr 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

Risposta accettata

Matt J
Matt J il 26 Apr 2021
Modificato: Matt J il 26 Apr 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 Commento
Jon Stapchuck
Jon Stapchuck il 26 Apr 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by