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

60 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

John BG
John BG am 12 Jan. 2017
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

Weitere Antworten (1)

John Chilleri
John Chilleri am 12 Jan. 2017
Bearbeitet: John Chilleri am 12 Jan. 2017
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 Kommentare
Victor Gruner
Victor Gruner am 29 Nov. 2021
It didn`t work for me.
when I edit the fzero file, my fzero output is always my initial value.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Historical Contests finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by