0

I'm confused about MATLAB's symbolic operation precedence. My code does not produce the desired output.

syms x1 x2 x3 x4
aa=((x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0) + ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000));
bb=eval(subs(aa, [x1 x2 x3 x4], [0.2 0.2 0.2 0.2]))

I expect the output of

bb=
    0.1051

but the actual output is

bb =
    logical
    0

Why does MATLAB do this? I use MATLAB R2018b

aliceW
  • 3
  • 2
  • Hi, I didnt get what does `aa=(a==b)+c` means... What do you want to express? – betontalpfa Jul 05 '19 at 07:36
  • Here is what I want. `(a==b)` should be computed first. The logical result `0` or `1` then is added to a fraction `c`. Thank you! – aliceW Jul 05 '19 at 07:51
  • If you have more information, please [edit] the question, instead of posting it in comments. Comment can be deleted fairly easily and are more difficult to format than the question body itself. In any case, why do you want this symbolic, and not numeric? – Adriaan Jul 05 '19 at 07:52
  • There's no need to go symbolic here, why do you use symbolic variables? Symbolic variables have a lot of limitations, with the most prominent ones being a significant slowdown as compared to numeric variables. Also: why not simply do it in two steps? Do the logical comparison first, then add the rest in the next step. – Adriaan Jul 05 '19 at 08:02
  • Thank you everyone. I have modified my question. This is the original problem.I didn’t expect it to be confusing. – aliceW Jul 05 '19 at 08:03
  • @Adriaan This is part of my whole question. It occurs when I use `messsage passing`+`junction tree` to get symbolic marginal probability distribution. Since there exists 0/0, I let the denominator `0` be `1` . – aliceW Jul 05 '19 at 08:10

2 Answers2

2
  • Case 1: x == y
syms x y z

aa = (x == y) + z

Meaning

aa = [x or y] + z = x + z = y + z

Wherever you have x, you can replace it by y, interchangeably.
It does not check if x and y are the same


  • Case 2: isequal(x, y)
syms x y z

aa = (x == y) + z
aa = isequal(x, y) + z

Meaning

aa = [check if x and y are the same] + z = 0 + z = z

Modify the given code to this using isequal()

syms x1 x2 x3 x4
aa=((isequal(x1, 0)& isequal(x2, 0) & isequal(x3, 0) & isequal(x4, 0)) + ...
    ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000));
bb=eval(subs(aa, [x1 x2 x3 x4], [0.2 0.2 0.2 0.2]));

Result

bb = 0.1051
Adam
  • 2,726
  • 1
  • 9
  • 22
1

I think MATLAB tries to reduce the number of brackets necessary to produce the simplest output. Note that if a is equal to b, a+c is also equal to b+c, so the statement is not wrong. If you call simplify(aa) you even get a simple a == b, which is indeed the simplest form as one can cancel the c on both sides:

syms a b c
aa=(a==b)+c
aa =
a + c == b + c
pretty(aa)
a + c == b + c

simplify(aa)
ans =
a == b

With respect to your edited question: why are you using symbolic variables at all? They are slow and cumbersome (not to speak about the unspeakable evil that is eval). Using numeric computation does yield the correct result:

syms x1 x2 x3 x4
aa=((x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0) + ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000));
bb=eval(subs(aa, [x1 x2 x3 x4], [0.2 0.2 0.2 0.2]))
bb =
  logical
   0

x1=0.2;x2=0.2;x3=0.2;x4=0.2;
(x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0)
ans =
  logical
   0

((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000)
ans =
    0.1051

aa=((x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0) + ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000))
aa =
    0.1051

I suspect the problem is somewhere in the belly of subs and/or eval.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Yes, the reason for this is `subs`. I have look into MATLAB, but I failed. I suspect there is some default setting that can't be modefied. In fact, this is part of my whole question. It occurs when I use `messsage passing`+`junction tree` to get symbolic marginal probability distribution. Since there exists `0/0`, I set the denominator `0` be `1` . When I input symbolic initial probability, `0/0` of symbolic type get this problem. – aliceW Jul 05 '19 at 08:27