Symbolic boolean function error at evaluation

4 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by