Inverting a Matrix
Copyright Kevin G. TeBeest
First load the "linalg" package for doing linear algebra:
>
with(linalg):
Example: Define a 4 x 4 matrix A:
>
A := matrix( [ [ 2, 8, –10, 4 ], [ 4, 13, –38,
–1 ],
[ 5, 22, –7, –26 ], [ 4, 19, 0, 0 ] ] ) ;
Calculate the determinant of A.
>
det( A ) ;
Since the determinant is not zero, A has an inverse.
Calculate the inverse of A,
and call it Ainv:
>
Ainv := inverse( A ) ;
Convert the inverse to decimal form:
>
evalf( evalm( Ainv ) ) ;
Define a constant vetor b:
>
b := vector( [ 16, –22, 46, 73 ] ) ;
We may solve the linear system Ax = b
by using the formula x = A–1b :
file: linsolve.mws
x := [4, 3, 2, 1]
Let's check: let's now solve the same system Ax = b using the "linsolve" command:
> x := linsolve( A, b ) ;
x := [4, 3, 2, 1]