Operands to the || and && operators must be convertible to logical scalar values. Need help

2 views (last 30 days)
A=1.202E6;
kB=1.38E23;
f=(1.5*1.6E-19:0.25*1.6E-19:7*1.6E-19);
J=2.735E8;
fzero(@(T) A*T^2*exp(-f/(kB*T))-J,2000)
I got this error messageOperands to the and && operators must be convertible to logical scalar valuesError in fzero (line 308) elseif ~isfinite(fx) ~isreal(fx)
Can anyone help me with this? Thanks

Answers (2)

Jan
Jan on 18 Oct 2015
The function must return a scalar value. Due to the vector f your function replies several outputs, wo how can fzero find a zero?
  1 Comment
Ryan Chen
Ryan Chen on 18 Oct 2015
Edited: Walter Roberson on 18 Oct 2015
A=1.202E6;
kB=1.38E23;
f=[1.5*1.6E-19:0.25*1.6E-19:7*1.6E-19];
J=2.735E8;
for i=1:23
T(i)=fzero(@(T) A*T^2*exp(-f(i)/(kB*T))-J,4000)
end
So I tried to use a for loop, the error message is gone but I received all the answers as NaN. What's wrong with my code. Really new to MATLAB, appreciate for your help.

Sign in to comment.


Walter Roberson
Walter Roberson on 18 Oct 2015
You did not constrain your answer at all, so there is nothing stopping fzero from looking at larger and larger T to try to find a zero crossing. Eventually you get to the point where you hit floating point overflow. Try guesses closer to 0.
There are two solutions that are the negatives of each other. If you do not want negative solutions than you can use 0 as the lower bound when you fzero.
The analytic solutions are at (1/2)*f(i)/(kB*LambertW( +/- (1/2)*sqrt(A*f(i)^2/(kB^2*J))))
The LambertW function is available in the Symbolic Toolbox under the name lambertW. See this blog

Categories

Find more on Financial Data Analytics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!