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:
For more details, the descriptions of these functions are:
isequal(A,B)
checks ifA
andB
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 value1
(true
) ifA
andB
are the same expressions. Otherwise, it returns logical0
(false
). Note thatisequal
does not considerNaN
(not a number) values as equal. To considerNaN
values as equal, you can useisequaln
.logical(cond)
checks if the symbolic statements incond
hold true without applying mathematical transformations and simplifications. It also ignores assumptions on symbolic variables.logical
returns a logical array with elements1
(true
) for the elements incond
that are true and0
(false
) for the elements incond
that are false.isAlways(cond)
checks if the symbolic statements incond
are always true for all possible values of the symbolic variables incond
. When verifyingcond
,isAlways
applies mathematical transformations and simplifications.isAlways
also considers all assumptions on the variables incond
.isAlways
returns a logical array with elements1
(true
) for the elements incond
that are mathematically true and0
(false
) for the elements incond
that are not mathematically true. In almost all cases, you can useisAlways
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 and , 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 , and use isequal
to test if the expanded expression is equal to .
expr = expand((x+1)^2)
expr =
tf = isequal(expr,x^2+2*x+1)
tf = logical
1
Next, check if the equation is true for all values of by using isAlways
.
tf = isAlways(tan(x) == sin(x)/cos(x))
tf = logical
1
Test if the expressions and 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 in terms of and . Test if rewrite
correctly rewrites as .
expr = rewrite(tan(x),"sincos")
expr =
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 and .
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 =
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 . Use isequal
to check if the simplified expression is equal to .
expr = simplify((x^2+2*x+1)/(x+1))
expr =
tf = isequal(x+1,expr)
tf = logical
1
Check Equation with Assumptions
Check if the equation holds true for all integers . When you create 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 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 AND . 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 =
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 OR . Instead, use isAlways
to test this conditional statement.
cond2 = sin(x)^2 + cos(x)^2 == 1 | x^2 > 0
cond2 =
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 =
expr2 = [sin(x)/cos(x); (x^2+2*x+1)/(x+1); x]
expr2 =
To compare these expressions, create a symbolic array of conditional statements using the relational operator ==
.
cond = expr1 == expr2
cond =
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 =
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