Error using fzero with vectors

7 ビュー (過去 30 日間)
Chris
Chris 2015 年 2 月 13 日
編集済み: per isakson 2015 年 2 月 13 日
Alright so I have this big huge complex formula and I am trying to solve it for AP2_AP1. My TA said I could use the fzero command and it would solve it for me. So I put it in a loop and I keep getting Operands to the and && operators must be convertible to logical scalar values.
Error in fzero (line 308)
elseif ~isfinite(fx) || ~isreal(fx)
AP4_AP1 is a vector and I expect AP2_AP1 to be a vector too. Not sure how to fix this.
f= @(AP2_AP1)AP2_AP1.*(1-((Gamma-1).*(AP2_AP1-1))/sqrt((2.*Gamma).*(2*Gamma+(Gamma+1).*(AP2_AP1-1)))).^((-2.*Gamma)/(Gamma-1))-AP4_AP1;
for i=1:1500
fzero(f, 1)
AW(i) = a*sqrt((Gamma+1)/(2*Gamma)*(AP1(i)/AP2(i)-1)+1); % Shock speed
end

回答 (1 件)

per isakson
per isakson 2015 年 2 月 13 日
編集済み: per isakson 2015 年 2 月 13 日
I think you should replace
(AP2_AP1-1))/sqrt
by
(AP2_AP1-1))./sqrt
to make f work with vectors.
&nbsp
Why do you expect AP2_AP1 is a vector? I think fzero requires that it is a scalar. However, I cannot find it stated in the documentation.
  3 件のコメント
Torsten
Torsten 2015 年 2 月 13 日
fzero gives scalar input and expects scalar output.
I guess you want the following:
for i=1:length(AP4_AP1)
f=@(x) x*(1-((Gamma-1)*(x-1))/sqrt((2*Gamma)*(2*Gamma+(Gamma+1).*(x-1))))^((-2*Gamma)/(Gamma-1))-AP4_AP1(i);
z=fzero(f,1);
AP2_AP1(i)=z;
end
I assumed gamma is a scalar. If gamma is a vector, replace gamma by gamma(i).
Best wishes
Torsten.
per isakson
per isakson 2015 年 2 月 13 日
編集済み: per isakson 2015 年 2 月 13 日
I looked a bit harder. The documentation on fzero, Function to solve, says "Function to solve, specified as a handle to a scalar-valued function. fun accepts a scalar x and returns a scalar fun(x)."
You need a code that uses fzero with one element of AP4_AP1 at a time. Did you try Torstens code?

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

カテゴリ

Help Center および File ExchangePowertrain Blockset についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by