Contenuto principale

Symbolic Matrix Computation

R2026b

This example shows how to perform symbolic matrix computations using Symbolic Math Toolbox™.

Generate a possibly familiar test matrix, the 5-by-5 Hilbert matrix.

H = sym(hilb(5)) 
H = 

(1121314151213141516131415161714151617181516171819)

The determinant is very small.

d = det(H) 
d = 

1266716800000

The elements of the inverse are integers.

X = inv(H) 
X = 

(25-3001050-1400630-3004800-1890026880-126001050-1890079380-11760056700-140026880-117600179200-88200630-1260056700-8820044100)

Verify that the inverse is correct.

I = X*H
I = 

(1000001000001000001000001)

Find the characteristic polynomial.

syms x; p = charpoly(H,x) 
p = 

x5-563 x4315+735781 x32116800-852401 x2222264000+61501 x53343360000-1266716800000

Try to factor the characteristic polynomial.

factor(p) 
ans = 

(1266716800000266716800000 x5-476703360000 x4+92708406000 x3-1022881200 x2+307505 x-1)

The result indicates that the characteristic polynomial cannot be factored over the rational numbers.

Compute the 50 digit numerical approximations to the eigenvalues.

digits(50) 
e = eig(vpa(H)) 
e = 

(1.56705069109823079553301100552072463394931525223340.208534218611013335905002510068820055038582022603430.011407491623419806559451458866589345042348430526640.000305898040151191726879497840692722825656145149092470.0000032879287721718629571150047605447313997367890230746)

Create a generalized Hilbert matrix involving a free variable, t.

t = sym('t'); 
[I,J] = meshgrid(1:5); 
H = 1./(I+J-t)
H = 

(-1t-2-1t-3σ5σ3σ1-1t-3σ5σ3σ1σ2σ5σ3σ1σ2σ4σ3σ1σ2σ4-1t-9σ1σ2σ4-1t-9-1t-10)where  σ1=-1t-6  σ2=-1t-7  σ3=-1t-5  σ4=-1t-8  σ5=-1t-4

Substituting t=1 retrieves the original Hilbert matrix.

subs(H,t,1) 
ans = 

(1121314151213141516131415161714151617181516171819)

The reciprocal of the determinant is a polynomial in t.

d = 1/det(H) 
d = 

-t-2 t-32 t-43 t-54 t-65 t-74 t-83 t-92 t-1082944

d = expand(d)
d = 

-t2582944+25 t2413824-5375 t2341472+40825 t226912-15940015 t2182944+21896665 t204608-240519875 t192592+1268467075 t18864-1588946776255 t1782944+2885896606895 t1613824-79493630114675 t1541472+34372691161375 t142304-8194259295156385 t1382944+7707965729450845 t1213824-55608098247105175 t1120736+37909434298793825 t103456-197019820623693025 t95184+10640296363350955 t896-38821472549340925 t7144+12958201048605475 t624-1748754621252377 t52+1115685328012530 t4-1078920141906600 t3+742618453752000 t2-323874210240000 t+67212633600000

The elements of the inverse are also polynomials in t.

X = inv(H) 
X = 

(-t-2 t-3 t-4 t-5 t-6 σ4576t-3 t-4 t-5 t-6 t-7 t4-17 t3+104 t2-268 t+240144-t-4 t-5 t-6 t-7 t-8 t4-16 t3+91 t2-216 t+18096t-5 t-6 t-7 t-8 t-9 t4-15 t3+80 t2-180 t+144144-t-6 t-7 t-8 t-9 t-10 t4-14 t3+71 t2-154 t+120576t-2 t-3 t-4 t-5 t-6 σ3144-t-3 t-4 t-5 t-6 t-7 t4-21 t3+161 t2-531 t+63036t-4 t-5 t-6 t-7 t-8 t4-20 t3+145 t2-450 t+50424-t-5 t-6 t-7 t-8 t-9 t4-19 t3+131 t2-389 t+42036t-6 t-7 t-8 t-9 t-10 σ4144-t-2 t-3 t-4 t-5 t-6 σ296t-3 t-4 t-5 t-6 t-7 t4-25 t3+230 t2-920 t+134424-t-4 t-5 t-6 t-7 t-8 t4-24 t3+211 t2-804 t+112016t-5 t-6 t-7 t-8 t-9 t4-23 t3+194 t2-712 t+96024-t-6 t-7 t-8 t-9 t-10 σ396t-2 t-3 t-4 t-5 t-6 σ1144-t-3 t-4 t-5 t-6 t-7 t4-29 t3+311 t2-1459 t+252036t-4 t-5 t-6 t-7 t-8 t4-28 t3+289 t2-1302 t+216024-t-5 t-6 t-7 t-8 t-9 t4-27 t3+269 t2-1173 t+189036t-6 t-7 t-8 t-9 t-10 σ2144-t-2 t-3 t-4 t-5 t-6 t4-34 t3+431 t2-2414 t+5040576t-3 t-4 t-5 t-6 t-7 t4-33 t3+404 t2-2172 t+4320144-t-4 t-5 t-6 t-7 t-8 t4-32 t3+379 t2-1968 t+378096t-5 t-6 t-7 t-8 t-9 t4-31 t3+356 t2-1796 t+3360144-t-6 t-7 t-8 t-9 t-10 σ1576)where  σ1=t4-30 t3+335 t2-1650 t+3024  σ2=t4-26 t3+251 t2-1066 t+1680  σ3=t4-22 t3+179 t2-638 t+840  σ4=t4-18 t3+119 t2-342 t+360

Substituting t=1 generates the Hilbert inverse.

X = subs(X,t,'1') 
X = 

(25-3001050-1400630-3004800-1890026880-126001050-1890079380-11760056700-140026880-117600179200-88200630-1260056700-8820044100)

X = double(X) 
X = 5×5

          25        -300        1050       -1400         630
        -300        4800      -18900       26880      -12600
        1050      -18900       79380     -117600       56700
       -1400       26880     -117600      179200      -88200
         630      -12600       56700      -88200       44100

Investigate a different example.

A = sym(gallery(5)) 
A = 

(-911-2163-25270-69141-4211684-575575-11493451-138013891-38917782-23345933651024-10242048-614424572)

This matrix is "nilpotent". Its fifth power is the zero matrix.

A^5 
ans = 

(0000000000000000000000000)

Because this matrix is nilpotent, its characteristic polynomial is very simple.

p = charpoly(A,'lambda') 
p = λ5

You should now be able to compute the matrix eigenvalues in your head. They are the zeros of the equation lambda^5 = 0.

Symbolic computation can find the eigenvalues exactly.

lambda = eig(A) 
lambda = 

(00000)

Numeric computation involves roundoff error and finds the zeros of an equation that is something like lambda^5 = eps*norm(A) So the computed eigenvalues are roughly lambda = (eps*norm(A))^(1/5) Here are the eigenvalues, computed by the Symbolic Toolbox using 16 digit floating point arithmetic. It is not obvious that they should all be zero.

digits(16) 
lambda = eig(vpa(A)) 
lambda = 

(0.00056174484863958470.0001737348850386136-0.0005342985684139864 i0.0001737348850386136+0.0005342985684139864 i-0.000454607309358406+0.0003303865815979566 i-0.000454607309358406-0.0003303865815979566 i)

This matrix is also "defective". It is not similar to a diagonal matrix. Its Jordan Canonical Form is not diagonal.

J = jordan(A) 
J = 

(0100000100000100000100000)

The matrix exponential, expm(t*A), is usually expressed in terms of scalar exponentials involving the eigenvalues, exp(lambda(i)*t). But for this matrix, the elements of expm(t*A) are all polynomials in t.

t = sym('t'); 
E = simplify(expm(t*A)) 
E = 

(-2 t33+11 t22-9 t+1t 4 t2-27 t+333-t 20 t2-117 t+1266t 32 t2-174 t+1893-t 85 t2-464 t+5042-t 7 t3-81 t2+230 t-14027 t4-67 t3+301 t22-69 t+1-t 35 t3-293 t2+598 t-2822t 112 t3-876 t2+1799 t-8422-t 1785 t3-14012 t2+28776 t-134728t 142 t3-1710 t2+5151 t-34506-t 142 t3-1426 t2+3438 t-17253355 t43-3139 t33+4585 t22-1149 t+1-t 1136 t3-9420 t2+20625 t-103533t 18105 t3-150646 t2+329952 t-16561212-t 973 t3-11675 t2+35022 t-233466t 1946 t3-19458 t2+46695 t-233466-t 4865 t3-42807 t2+93390 t-4669267784 t43-64210 t33+93391 t22-23345 t+1-t 248115 t3-2053748 t2+4482036 t-224076024-128 t t3-12 t2+36 t-243256 t t3-10 t2+24 t-123-128 t 5 t3-44 t2+96 t-483512 t 4 t3-33 t2+72 t-363-2720 t4+67552 t33-49144 t2+24572 t+1)

By the way, the function "exp" computes element-by-element exponentials.

X = exp(t*A) 
X = 

(e-9 te11 te-21 te63 te-252 te70 te-69 te141 te-421 te1684 te-575 te575 te-1149 te3451 te-13801 te3891 te-3891 te7782 te-23345 te93365 te1024 te-1024 te2048 te-6144 te24572 t)