Error using fzero with vectors
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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.
 
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
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
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?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!