Symbolic boolean function error at evaluation

조회 수: 8 (최근 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 CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by