Symbolic Math Toolbox |
 |
Linear Algebraic Operations
Let's do several basic linear algebraic operations.
The command
H = hilb(3)
generates the 3-by-3 Hilbert matrix. With format short
, MATLAB prints
H =
1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
The computed elements of H
are floating-point numbers that are the ratios of small integers. Indeed, H
is a MATLAB array of class double
. Converting H
to a symbolic matrix
H = sym(H)
gives
[ 1, 1/2, 1/3]
[1/2, 1/3, 1/4]
[1/3, 1/4, 1/5]
This allows subsequent symbolic operations on H
to produce results that correspond to the infinitely precise Hilbert matrix, sym(hilb(3))
, not its floating-point approximation, hilb(3)
. Therefore,
inv(H)
produces
[ 9, -36, 30]
[-36, 192, -180]
[ 30, -180, 180]
and
det(H)
yields
1/2160
We can use the backslash operator to solve a system of simultaneous linear equations. The commands
b = [1 1 1]'
x = H\b % Solve Hx = b
produce the solution
[ 3]
[-24]
[ 30]
All three of these results, the inverse, the determinant, and the solution to the linear system, are the exact results corresponding to the infinitely precise, rational, Hilbert matrix. On the other hand, using digits(16)
, the command
V = vpa(hilb(3))
returns
[ 1., .5000000000000000, .3333333333333333]
[.5000000000000000, .3333333333333333, .2500000000000000]
[.3333333333333333, .2500000000000000, .2000000000000000]
The decimal points in the representation of the individual elements are the signal to use variable-precision arithmetic. The result of each arithmetic operation is rounded to 16 significant decimal digits. When inverting the matrix, these errors are magnified by the matrix condition number, which for hilb(3)
is about 500. Consequently,
inv(V)
which returns
[ 9.000000000000082, -36.00000000000039, 30.00000000000035]
[-36.00000000000039, 192.0000000000021, -180.0000000000019]
[ 30.00000000000035, -180.0000000000019, 180.0000000000019]
shows the loss of two digits. So does
det(V)
which gives
.462962962962958e-3
and
V\b
which is
[ 3.000000000000041]
[-24.00000000000021]
[ 30.00000000000019]
Since H
is nonsingular, the null space of H
null(H)
and the column space of H
colspace(H)
produce an empty matrix and a permutation of the identity matrix, respectively. To make a more interesting example, let's try to find a value s
for H(1,1)
that makes H
singular. The commands
syms s
H(1,1) = s
Z = det(H)
sol = solve(Z)
produce
H =
[ s, 1/2, 1/3]
[1/2, 1/3, 1/4]
[1/3, 1/4, 1/5]
Z =
1/240*s-1/270
sol =
8/9
Then
H = subs(H,s,sol)
substitutes the computed value of sol
for s
in H
to give
H =
[8/9, 1/2, 1/3]
[1/2, 1/3, 1/4]
[1/3, 1/4, 1/5]
Now, the command
det(H)
returns
ans =
0
and
inv(H)
produces an error message
??? error using ==> inv
Error, (in inverse) singular matrix
because H
is singular. For this matrix, Z = null(H)
and C = colspace(H)
are nontrivial.
Z =
[ 1]
[ -4]
[10/3]
C =
[ 0, 1]
[ 1, 0]
[6/5, -3/10]
It should be pointed out that even though H
is singular, vpa(H)
is not. For any integer value d
, setting
digits(d)
and then computing
det(vpa(H))
inv(vpa(H))
results in a determinant of size 10^(-d)
and an inverse with elements on the order of 10^d
.
| Linear Algebra | | Eigenvalues |  |