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

1 回表示 (過去 30 日間)
Sergio Quesada
Sergio Quesada 2018 年 10 月 11 日
編集済み: Sergio Quesada 2018 年 10 月 16 日
Hi everybody!
Why am I having this error and how can I solve it?
I have already about this error but cannot find the way to avoid it in my case...
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
John D'Errico 2018 年 10 月 11 日
編集済み: John D'Errico 2018 年 10 月 11 日
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
  9 件のコメント
dpb
dpb 2018 年 10 月 15 日
編集済み: dpb 2018 年 10 月 15 日
Well, it (fzero) needs to be called such that the function only evaluates the one equation being solved for each time it is called.
ADDENDUM
As the output above shows, trapecios returns the same value for i=1:N; there's no reason for that to be there. Just remove the line
i = 1:N;
and the reference to (i) in the following to
Int=part.*(sum_me - (ue/2));
and your code should run. Now, whether fzero will find a solution I don't know; that is a really bizarre-looking functional and the initial go-round didn't look like it ever did have a zero-crossing quite possibly altho I've not looked at the above in detail.
It would probably be worth taking the time to plot the function(s) you're trying to solve versus a range of x values to see if they really are what you think they are supposed to be.
Sergio Quesada
Sergio Quesada 2018 年 10 月 16 日
編集済み: Sergio Quesada 2018 年 10 月 16 日
Solved due to your suggestion ! Thank you so much for your time and interest, dpb.

サインインしてコメントする。

その他の回答 (2 件)

the cyclist
the cyclist 2018 年 10 月 11 日
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.

dpb
dpb 2018 年 10 月 11 日
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.

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by