Fix Error: Operands to the || and && operators must be convertible to logical scalar values. Help Please.

조회 수: 9 (최근 30일)
Here is my code:
Re=linspace(2500,10^8,100);
G=10^(-4);
f0=64/2500;
f=fzero(@(f)((1/sqrt(f))+2*log10(G/3.7+2.51./(Re*sqrt(f)))),f0)
here is the error I get:
Operands to the and && operators must be convertible to logical scalar values.
Error in fzero (line 308) elseif ~isfinite(fx) ~isreal(fx)
Error in MatlabModule4 (line 12) f=fzero(@(f)((1/sqrt(f))+2*log10(G./3.7+2.51./(Re*sqrt(f)))),f0)
Any help is appreciated, thanks.

채택된 답변

John BG
John BG 2017년 1월 12일
you are not trying to pass 1 function to fsolve but 100 functions, which is the amount of components of variable Re
Just pass one at a time
f=fzero(@(f)((1/sqrt(f))+2*log10(G/3.7+2.51./(Re(1)*sqrt(f)))),f0)
f =
0.046137373253513
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

추가 답변 (1개)

John Chilleri
John Chilleri 2017년 1월 12일
편집: John Chilleri 2017년 1월 12일
Hello,
With investigation into the Fzero function, it seems you're producing a nonscalar fx. This leads me to believe that the problem lies with your function initialization, specifically with the Re. When you include a vector in a function initialization, and use a function that searches for a value that produces 0, it won't succeed because it's attempting to check a single value, not multiple. The error you are encountering is when a matrix/vector is put into a check meant for a scalar.
To avoid this, define your function to produce a single value when evaluated, which means you cannot have vectors or matrices (Re) in your function definition.
If you are sure that your function needs to remain as is, you can overwrite the fzero function to accommodate by doing as follows:
  • edit fzero
  • Replace all && with & and | | with | (use command f for search and replace)
Once you have done this, fzero will evaluate it as you have above.
f=fzero(@(f)((1/sqrt(f))+2*log10(G/3.7+2.51./(Re*sqrt(f)))),f0)
f =
0.0125
Hope this helps!
  댓글 수: 3
Victor Gruner
Victor Gruner 2021년 11월 29일
It didn`t work for me.
when I edit the fzero file, my fzero output is always my initial value.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by