Error using fzero with vectors
조회 수: 3 (최근 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?
참고 항목
카테고리
Help Center 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!