# Error using fzero with vectors

7 views (last 30 days)
Chris on 13 Feb 2015
Edited: per isakson on 13 Feb 2015
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

per isakson on 13 Feb 2015
Edited: per isakson on 13 Feb 2015
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.
per isakson on 13 Feb 2015
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?