Symbolic boolean function error at evaluation

6 ビュー (過去 30 日間)
timo
timo 2017 年 3 月 20 日
コメント済み: John Smith 2018 年 4 月 23 日
clear all
syms x b a
x(a,b) =xor(a,b)
y(a,b) = and(or(a,b),(not(and(a,b))))
x(0,0)
y(0,0)
I get an error at y(0,0). Can someone help ?
Error using symengine
The operand is invalid.
Error in sym/subs>mupadsubs (line 150)
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 135)
G = mupadsubs(F,X,Y);
Error in symfun/feval>evalScalarFun (line 42)
y = subs(formula(Ffun), Fvars, inds);
Error in symfun/feval (line 28)
varargout{1} = evalScalarFun(F, Fvars, varargin);
Error in symfun/subsref (line 175)
B = feval(A,inds{:});

回答 (2 件)

Renee Coetsee
Renee Coetsee 2017 年 3 月 27 日
I was able to isolate the error at y(0,0) to the "or(a,b)" function in "y(a,b)". You are getting this error because you need to use logical operators (and, or, ~, |, &, ...) with symbolic expressions and variables. Always make sure that you use logical expressions, such as:
y(a,b) = or(a~=0, b~=0);
"x(0,0) does not cause an error because "xor" in the Symbolic engine works not only on logical values, but also accepts numbers. This does not match other the other functions, so you should try to always use logical expressions.
  1 件のコメント
John Smith
John Smith 2018 年 4 月 23 日

This gives something unexpected:

>> subfn(x1,x2) = or(x1~=0, x2~=0); subfn(1,0)
ans =
0 ~= 0 | 1 ~= 0

And while it still converges to the correct value:

>> subfn(x1,x2) = or(x1~=0, x2~=0); simplify(subfn(1,0))
ans =
TRUE

I suppose it's not the best way of doing this.

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


John Smith
John Smith 2018 年 4 月 23 日
Symbolic Toolbox has some own weird boolean constants: TRUE and FALSE. I couldn't yet find the less tricky way of obtaining them than this:
TRUE = sym(1) | 1;
FALSE = sym(0) & 0;
Then if you do:
>> subfn(x1,x2) = or(x1, x2); subfn(1,0)
Error using symengine (line 59)
The operand is invalid.
But:
>> subfn(x1,x2) = or(x1, x2); subfn(FALSE,TRUE)
ans =
TRUE

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by