Search code examples
matlabsymbolic-math

MATLAB isequal function gives wrong answer for symbolic expression


In the following simple code:

syms x
isequal((x+1)^2, x^2+2*x+1)

MATLAB returns false, but two expressions are same! What is wrong with the code?


Solution

  • These are not exactly the same expressions, and isequal() tests for expression equality. Try for example:

    >> isequal(expand((x+1)^2), x^2+2*x+1)
    
    ans =
    
      logical
    
       1
    

    or,

    >> isequal(simplify((x+1)^2), simplify(x^2+2*x+1))
    
    ans =
    
      logical
    
       1
    

    PS you could also use isAlways() to compare expressions,

    isAlways((x+1)^2 == x^2+2*x+1)
    
    ans =
    
      logical
    
       1