# 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 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 ${\left(\mathit{x}+1\right)}^{2}$ and ${\mathit{x}}^{2}+2\mathit{x}+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 ${\left(\mathit{x}+1\right)}^{2}$, and use `isequal`

to test if the expanded expression is equal to ${\mathit{x}}^{2}+2\mathit{x}+1$.

expr = expand((x+1)^2)

`expr = $${x}^{2}+2\hspace{0.17em}x+1$$`

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

`tf = `*logical*
1

Next, check if the equation $\mathrm{tan}\left(\mathit{x}\right)=\frac{\mathrm{sin}\left(\mathit{x}\right)}{\mathrm{cos}\left(\mathit{x}\right)}$ is true for all values of $\mathit{x}$ by using `isAlways`

.

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

`tf = `*logical*
1

Test if the expressions $\mathrm{tan}\left(\mathit{x}\right)$ and $\frac{\mathrm{sin}\left(\mathit{x}\right)}{\mathrm{cos}\left(\mathit{x}\right)}$ 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 $\mathrm{tan}\left(\mathit{x}\right)$ in terms of $\mathrm{sin}\left(\mathit{x}\right)$ and $\mathrm{cos}\left(\mathit{x}\right)$. Test if `rewrite`

correctly rewrites $\mathrm{tan}\left(\mathit{x}\right)$ as $\frac{\mathrm{sin}\left(\mathit{x}\right)}{\mathrm{cos}\left(\mathit{x}\right)}$.

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

expr =$$\frac{\mathrm{sin}\left(x\right)}{\mathrm{cos}\left(x\right)}$$

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 $\mathit{x}+1$ and $({\mathit{x}}^{2}+2\mathit{x}+1)/\left(\mathit{x}+\mathrm{1}\right)$.

```
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 = $$\mathrm{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 $({\mathit{x}}^{2}+2\mathit{x}+1)/\left(\mathit{x}+\mathrm{1}\right)$. Use `isequal`

to check if the simplified expression is equal to $\mathit{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 $\mathrm{sin}\left(2\mathit{n}\pi \right)=0$ holds true for all integers $\mathit{n}$. When you create $\mathit{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 $\mathit{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 $\mathrm{exp}\left(\mathrm{log}\left(\mathit{x}\right)\right)=\mathit{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 ${\mathrm{sin}\left(\mathit{x}\right)}^{2}+{\mathrm{cos}\left(\mathit{x}\right)}^{2}=1$ OR ${\mathit{x}}^{2}>0$. Instead, use `isAlways`

to test this conditional statement.

cond2 = sin(x)^2 + cos(x)^2 == 1 | x^2 > 0

`cond2 = $$0<{x}^{2}\vee {\mathrm{cos}\left(x\right)}^{2}+{\mathrm{sin}\left(x\right)}^{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 =$$\left(\begin{array}{c}\mathrm{tan}\left(x\right)\\ x+1\\ x\end{array}\right)$$

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

expr2 =$$\left(\begin{array}{c}\frac{\mathrm{sin}\left(x\right)}{\mathrm{cos}\left(x\right)}\\ \frac{{x}^{2}+2\hspace{0.17em}x+1}{x+1}\\ x\end{array}\right)$$

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

.

cond = expr1 == expr2

cond =$$\left(\begin{array}{c}\mathrm{tan}\left(x\right)=\frac{\mathrm{sin}\left(x\right)}{\mathrm{cos}\left(x\right)}\\ x+1=\frac{{x}^{2}+2\hspace{0.17em}x+1}{x+1}\\ x=x\end{array}\right)$$

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 =$$\left(\begin{array}{c}\mathrm{tan}\left(x\right)\\ x+1\\ x\end{array}\right)$$

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