symbolic logical operation can't get right answer
2 views (last 30 days)
Show older comments
Hello,
I have
syms a b c
aa=(a==b)+c
bb=eval(subs(aa,[a,b,c], [1/5, 0, 1051/10000]))
My expected result is
aa=(a==b)+c
bb=
0.1051
However, the actual result is
aa =
a + c == b + c
bb =
logical
0
In my opinion, (a==b) should be computed first. The logical result then is added to c. Finally I can get a double number 0.1051. However, the actual result is not what I want.
How did that happen? What can I do to solve this problem?
0 Comments
Accepted Answer
Walter Roberson
on 5 Jul 2019
The comparison operators do not return 0 or 1 for the symbolic toolbox: they return constants TRUE or FALSE from DOM::Logical if I recall correctly.
If you want the relational operators to return 0 or 1 then you need to use piecewise(Condition, 1, 0)
Also, using eval() with symbolic expressions has undefined results.
MATLAB has three different programming languages. One is ordinary MATLAB. One is the MuPad engine language. The third is symbolic expressions, which are a hybrid between the other two and have incompatibilities with both of them. eval() applied to a symbolic expression will produce results that are not either MATLAB or MuPad in some situations.
3 Comments
Walter Roberson
on 5 Jul 2019
Why not? You have an equation and you add c to both sides of it. It is a common algebraic operation when solving equations, adding something to both sides of the equation, or multiplying or dividing both sides of the equation by a value.
Remember, (a==b) in symbolic toolbox is not going to give you a logical value.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!