Fix Error: Operands to the || and && operators must be convertible to logical scalar values. Help Please.
7 次查看(过去 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.
0 个评论
采纳的回答
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
2 个评论
John BG
2017-1-12
Anthony
would you please be so kind to mark my answer as accepted answer?
thanks for time and attention, awaiting answer
John BG
更多回答(1 个)
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
2021-11-29
It didn`t work for me.
when I edit the fzero file, my fzero output is always my initial value.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!