Main Content

Check Symbolic Equations, Inequalities, and Conditional Statements

Symbolic Math Toolbox™ provides several functions to check symbolic equations, inequalities, and conditional statements that involve symbolic objects. This example discusses the use cases of these functions:

  • Use isequal to check if two symbolic inputs are equal (from a coding perspective).

  • Use logical to check if symbolic conditions involving relational operators (such as &, |, >, ~=, and so on) are true.

  • Use isAlways to check if symbolic conditions are always mathematically true.

For more details, the descriptions of these functions are:

  • isequal(A,B) checks if A and B are the same size and their contents are equal (from a coding perspective). isequal is useful only to check equality between two expressions without applying mathematical transformations and simplifications. isequal returns a scalar logical value 1 (true) if A and B are the same expressions. Otherwise, it returns logical 0 (false). Note that isequal does not consider NaN (not a number) values as equal. To consider NaN values as equal, you can use isequaln.

  • logical(cond) checks if the symbolic statements in cond hold true without applying mathematical transformations and simplifications. It also ignores assumptions on symbolic variables. logical returns a logical array with elements 1 (true) for the elements in cond that are true and 0 (false) for the elements in cond that are false.

  • isAlways(cond) checks if the symbolic statements in cond are always true for all possible values of the symbolic variables in cond. When verifying cond, isAlways applies mathematical transformations and simplifications. isAlways also considers all assumptions on the variables in cond. isAlways returns a logical array with elements 1 (true) for the elements in cond that are mathematically true and 0 (false) for the elements in cond that are not mathematically true. In almost all cases, you can use isAlways to check symbolic equalities, inequalities, and conditional statements.

Check If Expressions Are Equal

isequal(a,b) only checks if a and b have the same contents but does not check if they are mathematically equal. If you use isequal to check different expressions, such as (x+1)2 and x2+2x+1, then it returns 0 (false), even though they are mathematically equal.

syms x
tf = isequal((x+1)^2,x^2+2*x+1)
tf = logical
   0

Expand the expression (x+1)2, and use isequal to test if the expanded expression is equal to x2+2x+1.

expr = expand((x+1)^2)
expr = x2+2x+1
tf = isequal(expr,x^2+2*x+1)
tf = logical
   1

Next, check if the equation tan(x)=sin(x)cos(x) is true for all values of x by using isAlways.

tf = isAlways(tan(x) == sin(x)/cos(x))
tf = logical
   1

Test if the expressions tan(x) and sin(x)cos(x) are equal. The isequal function returns 0 (false) because the expressions are different, even though they are mathematically equal.

tf = isequal(tan(x),sin(x)/cos(x))
tf = logical
   0

Rewrite the expression tan(x) in terms of sin(x) and cos(x). Test if rewrite correctly rewrites tan(x) as sin(x)cos(x).

expr = rewrite(tan(x),"sincos")
expr = 

sin(x)cos(x)

tf = isequal(expr,sin(x)/cos(x))
tf = logical
   1

Check Equation with and without Simplifications

To check an equation that requires simplifications, use isAlways. For example, check the equality of x+1 and (x2+2x+1)/(x+1).

syms x
tf = isAlways(x+1 == (x^2+2*x+1)/(x+1))
tf = logical
   1

If you use logical to check an equality with different expressions on both sides, then it returns 0 (false).

tf = logical(x+1 == (x^2+2*x+1)/(x+1))
tf = logical
   0

Simplify the condition represented by the symbolic equation using simplify. The simplify function returns the symbolic logical constant symtrue because the equation is always true for all values of x.

cond = simplify(x+1 == (x^2+2*x+1)/(x+1))
cond = symtrue

Using logical on symtrue converts it to logical 1 (true).

tf = logical(cond)
tf = logical
   1

As shown in the previous section, if you use isequal to check expressions that are different, then it returns 0 (false).

tf = isequal(x+1,(x^2+2*x+1)/(x+1))
tf = logical
   0

Simplify the expression (x2+2x+1)/(x+1). Use isequal to check if the simplified expression is equal to x+1.

expr = simplify((x^2+2*x+1)/(x+1))
expr = x+1
tf = isequal(x+1,expr)
tf = logical
   1

Check Equation with Assumptions

Check if the equation sin(2nπ)=0 holds true for all integers n. When you create n as a symbolic variable, Symbolic Math Toolbox treats it as a general complex quantity. To test if the equation holds true for integers, set an assumption on n and check the equation using isAlways.

syms n
assume(n,"integer")
tf = isAlways(sin(2*n*pi) == 0)
tf = logical
   1

Note that logical ignores assumptions on variables. It returns logical 0 (false) in this case.

tf = logical(sin(2*n*pi) == 0)
tf = logical
   0

Check Conditions Involving Equations and Inequalities

To check conditions involving equations and inequalities, you can use logical or isAlways. However, logical does not apply mathematical transformations and simplifications when checking the conditions.

For example, test the condition 1<2 AND exp(log(x))=x. Note that if a condition uses other functions, such as exp and log, then these functions are evaluated when defining the condition.

syms x
cond1 = 1 < 2 & exp(log(x)) == x
cond1 = x=x

Check this condition by using isAlways.

tf = isAlways(cond1)
tf = logical
   1

You can also use logical to check a condition that does not require mathematical transformations and simplifications.

tf = logical(cond1)
tf = logical
   1

Do not use logical to check if a condition holds true when mathematical transformations are required. For example, logical returns an error when testing the conditional statement sin(x)2+cos(x)2=1 OR x2>0. Instead, use isAlways to test this conditional statement.

cond2 = sin(x)^2 + cos(x)^2 == 1 | x^2 > 0
cond2 = 0<x2cos(x)2+sin(x)2=1
tf = isAlways(cond2)
tf = logical
   1

Check Multiple Conditions

To check multiple conditions, you can represent them as a symbolic array.

For example, create two symbolic arrays where each array has three different expressions.

syms x
expr1 = [tan(x); x+1; exp(log(x))]
expr1 = 

(tan(x)x+1x)

expr2 = [sin(x)/cos(x); (x^2+2*x+1)/(x+1); x]
expr2 = 

(sin(x)cos(x)x2+2x+1x+1x)

To compare these expressions, create a symbolic array of conditional statements using the relational operator ==.

cond = expr1 == expr2
cond = 

(tan(x)=sin(x)cos(x)x+1=x2+2x+1x+1x=x)

Check if these multiple conditions are always mathematically true using isAlways. isAlways returns a 3-by-1 array with logical values 1 (true) because each condition is mathematically true.

tf = isAlways(cond)
tf = 3x1 logical array

   1
   1
   1

Check if these conditions hold true using logical. logical returns a 3-by-1 array, where the first two elements are 0 (false) because logical does not apply mathematical transformations or simplifications.

tf = logical(cond)
tf = 3x1 logical array

   0
   0
   1

Check if each corresponding element in the two 3-by-1 symbolic arrays, expr1 and expr2, is equal using isequal. isequal returns a logical scalar 0 (false) because some of the corresponding elements are not equal.

tf = isequal(expr1,expr2)
tf = logical
   0

Next, simplify the second symbolic array using simplify.

expr2 = simplify(expr2,Steps=10)
expr2 = 

(tan(x)x+1x)

Check if each simplified expression in expr2 is equal to the corresponding expression in expr1 using logical.

tf = logical(expr1 == expr2)
tf = 3x1 logical array

   1
   1
   1

Check if all simplified expressions in expr2 are equal to expr1 using isequal.

tf = isequal(expr1,expr2)
tf = logical
   1

See Also

| | |