Search code examples
matlabequalitysymbolic-math

Checking Symbolic Equation Equality in Matlab


How can one check if symbolic eq1 is equal to symbolic eq2 in Matlab:

syms a x y
eq1 = a*(x+y)
eq2 = a*x + a*y

Solution

  • You could use simplify on the difference, then logical to test if it is equal to 0:

    eqAreEqual = logical(simplify(eq1-eq2) == 0);
    

    You could also use simplify on each and isequal for comparison:

    eqAreEqual = isequal(simplify(eq1), simplify(eq2));
    

    For example:

    >> syms a x y
    >> eq1 = a*(x+y);
    >> eq2 = a*x + a*y;
    >> eqAreEqual = logical(simplify(eq1-eq2) == 0)
    
    eqAreEqual =
    
      logical
    
       1  % True!
    
    >> eqAreEqual = isequal(simplify(eq1), simplify(eq2))
    
    eqAreEqual =
    
      logical
    
       1  % Also true!