Why does MatLab keep reversing the sign of my symbolic equation

12 views (last 30 days)
I'm trying to assign a variable with a series of symbolic variables, (E1 = E/(1-v^2)), and the output keeps returning -E/(v^2-1). This is technically mathematically sound but it's incredibly irritating.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Oct 2016
Experimentally, it appears that the symbolic engine has some preferred orders for commutative expressions. For example it will rewrite 1 + a to a + 1 and b + a to a + b .
Experimentally, it appears that the rule for a sum of terms in isolation is something like:
  • first, rational numeric constants are to be combined
  • then any floating point constants are to be combined with the rational result (or 0), in order from first to last in the expression
  • then, the addends are to be sorted alphabetically, with the numbers at the end, each term with its proper sign attached
  • then, the first positive term on the list is to be moved to the beginning; if no terms are positive then nothing is moved
  • the result is then in that order, with appropriate sign between addends
The above description needs to be expanded to describe the handling of numeric constants like sin(8/9) and of function calls like sin(x). Also I might be wrong on the order of combining floating point numbers. Note too that floating point numbers at the MATLAB level are generally converted to rational before being processed by the symbolic toolbox.
Experimentally, it appears that the rule for a sum of terms appearing in a denominator is different, and although I can see there is a pattern, I cannot as yet explain it, there are aspects that do not make sense at the moment. However one aspect that seems clear is that if it determines that the first addend to write into the denominator expression should be negative, then it will take the negative of the expression and multiply the numerator by -1, resulting in the ordering you see. At the moment, why it chooses that order is a mystery. For example,
>> evalin(symengine, '1/_plus(-a,5/2,b)')
ans =
1/(b - a + 5/2)
>> evalin(symengine, '1/_plus(-a,-5/2,b)')
ans =
-1/(a - b + 5/2)
It is not clear why the sign of the constant term would end up being the determining factor here rather than the alphabetic order of the terms or the first positive term in alphabetic order.
I would not count on the order being consistent -- though my recollection from several years ago is that the consistency is one of the interesting differences with Maple, with the MuPAD engine being consistent where Maple is not. (In Maple, the order was traditionally determined in part by the first ordering of the terms that was encountered during execution, so if the first thing you typed in your session was b + a then afterwards in the same session it would reorder a + b to be b + a)
  4 Comments
Walter Roberson
Walter Roberson on 18 Apr 2019
Could you give an example of a negative before a parenthesis causing an undesired different meaning ?
By the way, the symbolic engine was written at University of Paderborn and further developed by another company named SciFace, before eventually being purchased by Mathworks. It seems to me that the university would have been worried about getting correct results.
Steven Lord
Steven Lord on 18 Apr 2019
It seems to me that the university would have been worried about getting correct results.
It's not just the university. MathWorks also cares about getting correct results. Silent wrong answers are the most severe category of bugs in our bug database. In the particular case S H cited the answer Symbolic Math Toolbox displayed is correct, it is simply not in the form the user wanted.
While I could see that as causing a problem if the user is writing the result to a file to be compared against another file containing expected results, in that scenario I'd probably import the expected results from the file into MATLAB and use the isAlways function to determine if the two expressions are always equal.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!