Main Content

Test equality of symbolic inputs

`isequal(`

returns
logical `a,b`

)`1`

(`true`

) if `A`

and `B`

are
the same size and their contents are of equal value. Otherwise, `isequal`

returns
logical `0`

(`false`

). `isequal`

does
not consider `NaN`

(not a number) values equal. `isequal`

recursively
compares the contents of symbolic data structures and the properties
of objects. If all contents in the respective locations are equal, `isequal`

returns
logical `1`

(`true`

).

`isequal(`

returns
logical `a1,a2,...,aN`

)`1`

(`true`

) if all the
inputs `a1,a2,...,aN`

are equal.

Test numeric or symbolic inputs for equality
using `isequal`

. If you compare numeric inputs
against symbolic inputs, `isequal`

returns `0`

(`false`

)
because double and symbolic are distinct data types.

Test if `2`

and `5`

are equal.
Because you are comparing doubles, the MATLAB^{®} `isequal`

function is called. `isequal`

returns `0`

(`false`

)
as expected.

isequal(2,5)

ans = logical 0

Test if the solution of the equation `cos(x) == -1`

is `pi`

.
The `isequal`

function returns `1`

(`true`

)
meaning the solution is equal to `pi`

.

syms x sol = solve(cos(x) == -1, x); isequal(sol,sym(pi))

ans = logical 1

Compare the double and symbolic representations of `1`

. `isequal`

returns `0`

(`false`

)
because double and symbolic are distinct data types. To return `1`

(`true`

)
in this case, use `logical`

instead.

usingIsEqual = isequal(pi,sym(pi)) usingLogical = logical(pi == sym(pi))

usingIsEqual = logical 0 usingLogical = logical 1

Test if `rewrite`

correctly
rewrites `tan(x)`

as `sin(x)/cos(x)`

.
The `isequal`

function returns `1`

(`true`

)
meaning the rewritten result equals the test expression.

syms x f = rewrite(tan(x),'sincos'); testf = sin(x)/cos(x); isequal(f,testf)

ans = logical 1

Test vectors and matrices for equality using `isequal`

.

Test if solutions of the quadratic equation found by `solve`

are
equal to the expected solutions. `isequal`

function
returns `1`

(`true`

) meaning the
inputs are equal.

syms a b c x eqn = a*x^2 + b*x + c; Sol = solve(eqn, x); testSol = [-(b+(b^2-4*a*c)^(1/2))/(2*a); -(b-(b^2-4*a*c)^(1/2))/(2*a)]; isequal(Sol,testSol)

ans = logical 1

The Hilbert matrix is a special matrix that is difficult to invert accurately. If the inverse is accurately computed, then multiplying the inverse by the original Hilbert matrix returns the identity matrix.

Use this condition to symbolically test if the inverse of `hilb(20)`

is
correctly calculated. `isequal`

returns `1`

(`true`

)
meaning that the product of the inverse and the original Hilbert matrix
is equal to the identity matrix.

H = sym(hilb(20)); prod = H*inv(H); eye20 = sym(eye(20)); isequal(prod,eye20)

ans = logical 1

`NaN`

Compare three vectors containing `NaN`

(not
a number). `isequal`

returns logical `0`

(`false`

)
because `isequal`

does not treat `NaN`

values
as equal to each other.

syms x A1 = [x NaN NaN]; A2 = [x NaN NaN]; A3 = [x NaN NaN]; isequal(A1, A2, A3)

ans = logical 0

When your inputs are not symbolic objects, the MATLAB

`isequal`

function is called. If one of the arguments is symbolic, then all other arguments are converted to symbolic objects before comparison, and the symbolic`isequal`

function is called.