Symbolic boolean function error at evaluation

10 vues (au cours des 30 derniers jours)
timo
timo le 20 Mar 2017
Commenté : John Smith le 23 Avr 2018
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{:});

Réponses (2)

Renee Coetsee
Renee Coetsee le 27 Mar 2017
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 commentaire
John Smith
John Smith le 23 Avr 2018

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.

Connectez-vous pour commenter.


John Smith
John Smith le 23 Avr 2018
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

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by