Math 241 - Maple Workshop
Fall Semester, 2002
Matrices

 © 2001, All Rights Reserved, SDSU & Joseph M. Mahaffy
San Diego State University -- This page last updated 11-Sep-02


 Matrices

Matrices - Matrices, including basic operations, solving linear systems, and eigenvalues and eigenvectors.

This session introduces matrices and some basic operations in linear algebra. A hyperlink allows you to download the Maple worksheet.

Linear Algebra

The text has an excellent section introducing linear algebra. This can be found in Chapters 6 and 7, which provides the basic commands for linear algebra. You begin any session where you are going to ask Maple to perform operations in linear algebra by entering the Maple linear algebra package. (By using a ; instead of a :, you see the functions that this particular package has and it is an extensive package. Some of these topics will be familiar, but many may not be. Once again, the Maple help can be used to learn more about a particular topic of interest and how to use the Maple package.)

 

> with(linalg);

 

Warning, the protected names norm and trace have been redefined and unprotected

 

[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp,...

Basic Matrix Operations

There are several ways that you can enter a matrix.

> A := matrix([[1, -2, -4],[0, 3, 5]]);

A := matrix([[1, -2, -4], [0, 3, 5]])

> B := matrix(2, 3, [4, -2, 0, 7, 2, -3]);

B := matrix([[4, -2, 0], [7, 2, -3]])

If your matrix has a certain structure that can be easily programmed in, then the array command with a simple Maple program is an easier way to produce your matrix. The array command creates the dimensions of the matrix, then you can write a program to fill in the entries. Maple has for and do loops as shown below. A do loop is ended with the command od:

 

> C := array(1..2,1..3):

> for i to 2 do

> for j to 3 do

> C[i,j] := x^i + j - i

> od: od:

> print(C);

matrix([[x, x+1, x+2], [x^2-1, x^2, x^2+1]])

> evalm(2*A + B + C);

matrix([[6+x, -5+x, -6+x], [6+x^2, 8+x^2, 8+x^2]])

Note that Maple does have several special matrix forms that can be entered more easily with other commands, such as a diagonal matrix.

> Q := diag(4, -5, 3);

Q := matrix([[4, 0, 0], [0, -5, 0], [0, 0, 3]])

Matrix multiplication requires &*. Clearly, the matrices above cannot be multiplied together, but we can multiply the transpose of one with another.

 

> F := evalm(A&*transpose(B)); G := evalm(transpose(A)&*C);

F := matrix([[8, 15], [-6, -9]])

G := matrix([[x, x+1, x+2], [-2*x+3*x^2-3, -2*x-2+3...

Other matrix operations are very easy with many of the commands fairly obvious.

> inverse(F);

matrix([[-1/2, -5/6], [1/3, 4/9]])

> det(F); det(G); Note that G is singular.

18

0

> trace(F);

-1

Solving Linear Systems

Maple has a couple ways to solve linear systems of equations. Suppose we want to solve the linear system Ax = b, where

> A := matrix(3,3,[1, 0, 2, 3, 4, -2, 2, -1, 0]); b := [2, 6, -3];

A := matrix([[1, 0, 2], [3, 4, -2], [2, -1, 0]])

b := [2, 6, -3]

> x := linsolve(A, b);

x := vector([-1/3, 7/3, 7/6])

An alternate method using Gaussian elimination can be done as follows:

> A1 := augment(A, b);

A1 := matrix([[1, 0, 2, 2], [3, 4, -2, 6], [2, -1, ...

> B := gausselim(A1);

B := matrix([[1, 0, 2, 2], [0, 4, -8, 0], [0, 0, -6...

> x := backsub(B);

x := vector([-1/3, 7/3, 7/6])

Maple readily handles the infinite or no solution cases for linear systems also.

> A2 := matrix(3,3,[2, -1, 4, 3, 0, 1, 1, 1, -3]);

> b1 := [2, 4, 2]; b2 := [1, 3, -1];A2 := matrix([[2, -1, 4], [3, 0, 1], [1, 1, -3]])

b1 := [2, 4, 2]

b2 := [1, 3, -1]

> x1 := linsolve(A2,b1);

x1 := vector([_t[1], -10*_t[1]+14, -3*_t[1]+4])

> x2 := linsolve(A2, b2);

x2 := NULL

> rank(A2);

2

Eigenvalues and Eigenvectors

Very useful information about matrices concerns their eigenvalues and eigenvectors.

> eig := eigenvectors(A);

eig := [3, 1, {vector([1, -1, 1])}], [4, 1, {vector...

The response gives the value of each eigenvalue, its multiplicity, and its corresponding eigenvector. We can readily acquire any of these components as follows:

> eig[2];

[4, 1, {vector([1, -4, 3/2])}]

> eig[3][3];

{vector([-1, 1, 3/2])}

The characteristic matrix and characteristic polynomial are found with the following commands.

> charmat(A, lambda); Characteristic matrix in the form lI - A

matrix([[lambda-1, 0, -2], [-3, lambda-4, 2], [-2, ...

> charpoly(A, lambda); Characteristic polynomial det(lI - A)

lambda^3-5*lambda^2-2*lambda+24

> roots(%, lambda); Eigenvalues and multiplicities

[[3, 1], [4, 1], [-2, 1]]