Using 'fzero' in a for loop

1 回表示 (過去 30 日間)
Jon Stapchuck
Jon Stapchuck 2021 年 4 月 26 日
コメント済み: Jon Stapchuck 2021 年 4 月 26 日
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

採用された回答

Matt J
Matt J 2021 年 4 月 26 日
編集済み: Matt J 2021 年 4 月 26 日
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 件のコメント
Jon Stapchuck
Jon Stapchuck 2021 年 4 月 26 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by