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

1 次查看(过去 30 天)
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

回答(2 个)

Jan
Jan 2015-10-18
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 个评论
Ryan Chen
Ryan Chen 2015-10-18
编辑:Walter Roberson 2015-10-18
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2015-10-18
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

类别

Help CenterFile Exchange 中查找有关 Financial Data Analytics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by