Main Content

Linear Algebraic Operations

Symbolic Hilbert Matrix

The following examples, which show how to perform basic linear algebraic operations, are based on a symbolic version of the 3-by-3 Hilbert matrix.

Generate the 3-by-3 Hilbert matrix. With format short, MATLAB® prints the output shown.

H = hilb(3)
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. H is a MATLAB array of class double.

Convert H to a symbolic matrix.

H = sym(H)
H =
[   1, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]

Symbolic Linear Algebra Operations

Symbolic operations on H produce results that correspond to the infinite-precision Hilbert matrix, sym(hilb(3)), not its floating-point approximation, hilb(3).

Find the inverse of H.

inv(H)
ans =
[   9,  -36,   30]
[ -36,  192, -180]
[  30, -180,  180]

Find the determinant of H.

det(H)
ans =
1/2160

You can use the backslash operator to solve a system of simultaneous linear equations. For example, solve H*x = b.

b = [1; 1; 1];
x = H\b
 x =
  3
 -24
  30

All three results—the inverse, the determinant, and the solution to the linear system—are the exact results corresponding to the infinite-precision, rational, Hilbert matrix.

Variable-Precision Arithmetic

Contrast the preceding operations with variable-precision arithmetic using 20 digits of precision.

digits(20)
V = vpa(H)
V =
 
[                    1.0,                    0.5, 0.33333333333333333333]
[                    0.5, 0.33333333333333333333,                   0.25]
[ 0.33333333333333333333,                   0.25,                    0.2]

The decimal points in the representation of the individual elements indicate that MATLAB is using variable-precision arithmetic. The result of each arithmetic operation is rounded to 20 significant decimal digits.

Invert the matrix and note that errors are magnified by the matrix condition number, which for hilb(3) is about 500.

cond(V)
ans =
 
524.0567775860608

Compute the difference of the inverses of the infinite-precision and variable-precision versions.

ih = inv(H)
ih =
 
[   9,  -36,   30]
[ -36,  192, -180]
[  30, -180,  180]
iv = inv(V)
iv =
 
[   9.0,  -36.0,   30.0]
[ -36.0,  192.0, -180.0]
[  30.0, -180.0,  180.0]

Although these matrices look the same, calculate the difference to see that they are not.

dhv = ih - iv
dhv =
 
[ -5.4929962552349494034e-26,  2.4556924435168009098e-25, -2.1971985020939797614e-25]
[  2.4556924435168009098e-25, -1.2666203129718236271e-24,  1.1373733422604130529e-24]
[ -2.1971985020939797614e-25,  1.1373733422604130529e-24, -1.0856745539758488233e-24]

Solve the equation V*y = b. The answer looks the same as the solution to H*x = b.

y = V\b
y =
 
   3.0
 -24.0
  30.0

Calculate the difference between x and y to see the small difference between the two solutions.

x-y
ans =
 
  8.0779356694631608874e-27
 -6.4623485355705287099e-26
  7.1085833891275815809e-26

Using vpa with digits(16) offers comparable precision to using standard double-precision MATLAB routines.

Symbolic Investigation of Singular Value

Find a value s for H(1,1) that makes H singular.

syms s
Hs = H;
Hs(1,1) = s
Z = det(Hs)
sol = solve(Z)
Hs =
[   s, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]

Z =
s/240 - 1/270

sol = 
8/9

Substitute the solution for s into Hs.

Hs = subs(Hs, s, sol)
Hs =
[ 8/9, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]

Verify that the determinant of Hs is zero.

det(Hs)
ans =
0

Find the null space and column space of Hs. Both spaces are nontrivial.

N = null(Hs)
C = colspace(Hs)
N=
3/10
 -6/5
    1

C =
[     1,     0]
[     0,     1]
[ -3/10,   6/5]

Check that N is in the null space of Hs.

Hs*N
ans =
 
 0
 0
 0