Find rank of symbolic matrix

syms a b c d A = [a b; c d]; rank(A)

ans = 2

Symbolic calculations return the exact rank
of a matrix while numeric calculations can suffer from round-off errors.
This exact calculation is useful for ill-conditioned matrices, such
as the Hilbert matrix. The rank of a Hilbert matrix of order *n* is *n*.

Find the rank of the Hilbert matrix of order `15`

numerically.
Then convert the numeric matrix to a symbolic matrix using `sym`

and
find the rank symbolically.

H = hilb(15); rank(H) rank(sym(H))

ans = 12 ans = 15

The symbolic calculation returns the correct rank of `15`

.
The numeric calculation returns an incorrect rank of `12`

due
to round-off errors.

Consider this matrix

$$A=\left[\begin{array}{cc}1-{\mathrm{sin}}^{2}\left(x\right)& {\mathrm{cos}}^{2}\left(x\right)\\ 1& 1\end{array}\right].$$

After simplification of `1-sin(x)^2`

to `cos(x)^2`

,
the matrix has a rank of `1`

. However, `rank`

returns
an incorrect rank of `2`

because it does not take
into account identities satisfied by special functions occurring in
the matrix elements. Demonstrate the incorrect result.

syms x A = [1-sin(x) cos(x); cos(x) 1+sin(x)]; rank(A)

ans = 2

`rank`

returns an incorrect result because
the outputs of intermediate steps are not simplified. While there
is no fail-safe workaround, you can simplify symbolic expressions
by using numeric substitution and evaluating the substitution using `vpa`

.

Find the correct rank by substituting `x`

with
a number and evaluating the result using `vpa`

.

rank(vpa(subs(A,x,1)))

ans = 1

However, even after numeric substitution, `rank`

can
return incorrect results due to round-off errors.