# Operands to the || and && operators must be convertible to logical scalar values

5 views (last 30 days)
Sergio Quesada on 11 Oct 2018
Edited: Sergio Quesada on 16 Oct 2018
Hi everybody!
Why am I having this error and how can I solve it?
Thanks!!!
options=optimset('Display','iter');
fzero (@(r) trapecios(r)-100, 1.21, options)
function Int = trapecios (r)
FC = 4.24E-1; % 0.424 para 25 V y 0.381 para 40 V
a = 6.05; % 6.050 para 25 V y 4.730 para 40 V
%FD = 8.17E-4;
%Io = 15.53;
V = 25;
%N=10;
F = @(x) FC .* exp( -(V.*a) ./ (30.*x.*log(x)) );
Int = 0;
part = 1e-3;
L = 1:part:r;
me = F(L);
sum_me = sum (me);
ue = F( max( L ) );
Int = part.* (sum_me - (ue/2));
end
The error is:
Operands to the || and && operators must be convertible to logical scalar
values.
Error in fzero (line 365)
if ~isfinite(fa) || ~isreal(fa) || ~isfinite(a)
Error in Cesar_r_teor_trapecios (line 4)
fzero (@(k) trapecios(k)-100, 1.21, options)

John D'Errico on 11 Oct 2018
Edited: John D'Errico on 11 Oct 2018
LEARN TO USE THE DEBUGGER!!!!!!!!!
I did this:
dbstop if error
Next, I ran your code. Well, I tried to run it.
fzero (@(r) trapecios(r)-100, 1.21, options)
Search for an interval around 1.21 containing a sign change:
Func-count a f(a) b f(b) Procedure
1 1.21 -100 1.21 -100 initial interval
3 1.17578 -100 1.24422 -100 search
5 1.1616 -100 1.2584 -100 search
7 1.14155 -100 1.27845 -100 search
9 1.1132 -100 1.3068 -100 search
11 1.0731 -100 1.3469 -100 search
13 1.0164 -100 1.4036 -100 search
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 365)
if ~isfinite(fa) || ~isreal(fa) || ~isfinite(a)
365 if ~isfinite(fa) || ~isreal(fa) || ~isfinite(a)
What is fa now?
K>> whos fa
Name Size Bytes Class Attributes
fa 1x0 0 double
fa seems to be empty.
So there are clearly some values of r, such that when your function is called, return an empty result.
Again, while I am still in the debugger, look at a
a
a =
0.936208254324569
K>> trapecios(a)
ans =
1×0 empty double row vector
When I pass that number into your function, I get an empty result. Why does that happen? THINK!!!!!
part = 1e-3;
L = 1:part:r;
r is less than 1. What is the vector
1:0.001:.93
ans =
1×0 empty double row vector
So you need to learn to use the debugger.
How do you fix it? You could recognize that fzero allows you to pass in TWO numbers. That will allow you to bound the lower limit above 1. The problem is then fzero will insist that the two limits to bracket a root.
So simpler as a fix is to put a patch into your objective. At the very beginning of the code, add this:
if r <= 1
Int = 0;
return
end
Sergio Quesada on 16 Oct 2018
Solved due to your suggestion ! Thank you so much for your time and interest, dpb.

the cyclist on 11 Oct 2018
The specific reason you get that error is that fzero attempts to call your function trapecio with an argument value of about 0.932, and your function returns an empty vector in that case.
Sergio Quesada on 11 Oct 2018
Thanks !!

dpb on 11 Oct 2018
OK, this took a little while to debug... :)
The problem is in function trapecios, if the input argument is <1 then
L=1:part:r;
returns an empty vector and the function also returns an empty vector.
That triggers the error as the coders didn't envision the user function returning empty and didn't trap the condition for a unique error message.
I don't know what you want the function to return for that case, but it MUST return SOMETHING.
Sergio Quesada on 11 Oct 2018
Thank you so much, bdp ! ;)