Filter löschen
Filter löschen

Using 'fzero' in a for loop

3 Ansichten (letzte 30 Tage)
Jon Stapchuck
Jon Stapchuck am 26 Apr. 2021
Kommentiert: Jon Stapchuck am 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

Akzeptierte Antwort

Matt J
Matt J am 26 Apr. 2021
Bearbeitet: Matt J am 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 Kommentar
Jon Stapchuck
Jon Stapchuck am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by