OnLine1-4-GaussElim.mws

Linear Algebra Powertool

Gaussian Elimination

On Line Section 1.4

Worksheet written by Michael K. May, S.J., revised by Russell Blyth

> restart: with(linalg): with(plots): with(plottools):

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

Warning, the name changecoords has been redefined

Warning, the name arrow has been redefined

Outline

This worksheet goes through the problems covered in the On Line 1.4 section.

The basic objectives are:

Part1: Converting between systems of equations and matrices

The book points out that the Gaussian elimination method we used on systems of equations can be performed in shorthand by working on the matrix of coefficients of the system. In Maple the command for converting from a system of equations to a matrix is genmatrix. To convert back from a matrix to a system of equations, the command is geneqns. Each of these commands has two forms, one for the matrix of coefficeients, and one for the augmented matrix. These commands are part of the linalg package.

To convert from a system of equations to a matrix we first need a list of equations, and an ordered list of variables.

> eq1 := x + y + 2*z + w = 1;
eq2 := 3*x - 4*y + z + w = 2;
eq3 := 4*x - 3*y + 3*z + 2*w = 3;
eqlist := [eq1, eq2, eq3];
varlist := [x, y, z, w];

eq1 := x+y+2*z+w = 1

eq2 := 3*x-4*y+z+w = 2

eq3 := 4*x-3*y+3*z+2*w = 3

eqlist := [x+y+2*z+w = 1, 3*x-4*y+z+w = 2, 4*x-3*y+...

varlist := [x, y, z, w]

If we want the matrix of coefficients, the command genmatrix has two parameter: the list of equations and the list of variables. If we want the augmented matrix, we add a third parameter, the name `flag` (note the backquotes, found on the key to the left of the "1" key, top left of the keyboard).

> M1 := genmatrix(eqlist, varlist);
M2 := genmatrix(eqlist, varlist,`flag`);

M1 := matrix([[1, 1, 2, 1], [3, -4, 1, 1], [4, -3, ...

M2 := matrix([[1, 1, 2, 1, 1], [3, -4, 1, 1, 2], [4...

If we have the augmented matrix, we can use the delcols command to remove the last column (we ask for columns 5 through 5 to be removed) to obtain the coefficient matrix. The col command is used to turn the last column into a vector of values.

> M2a := delcols(M2,5..5);
M2b := col(M2,5);

M2a := matrix([[1, 1, 2, 1], [3, -4, 1, 1], [4, -3,...

M2b := vector([1, 2, 3])

To convert back from a matrix to a system of equations we use the geneqns command. To produce a system with the constants set to zero (that is, a homogeneous system) we use two parameters: the matrix and the variable list. For a nonhomogeneous system, we use a third parameter, a vector of constants.

> homsys := geneqns(M1,varlist);
nonhomsys := geneqns(M2a,varlist,M2b);

homsys := {x+y+2*z+w = 0, 3*x-4*y+z+w = 0, 4*x-3*y+...

nonhomsys := {3*x-4*y+z+w = 2, x+y+2*z+w = 1, 4*x-3...

>

Exercises:

1) Use Maple to convert the system{x-y+2z-2w=1, 2x+y+3w=4, 2x+3y+2z=6} to an augmented matrix.

>

2) Use Maple to convert the augmented matrix matrix([[0, 0, 2, 4, 1], [3, 1, 2, 6, 0], [1, 1, 1,... to a system of equations in x, y, z, and w.

>

3) Use the command N1 := randmatrix(3, 5); to generate a random 3 by 5 matrix named N1. Convert it to a system of equations in x, y, z, and w.

>

Part2: Elementary row operations

Once we have a system of equations converted to an augmented matrix, the next task is to use elementary row operations to perform Gaussian elimination on the matrix. The linalg package of Maple has a command corresponding to each type of elementary row operation. The command

addrow(M, r1, r2, scal);

is used to add scal times row r1 of matrix M to row r2. The command

mulrow(M, r1, scal);

is used to multiply row r1 of M by scal. The command

swaprow(M, r1, r2);

is used to switch rows r1 and r2 of the matrix M.

We use these operations to reduce the matrix M2 above to row echelon form.

> print(M2);

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

> M3a := addrow(M2,1,2,-3);

M3a := matrix([[1, 1, 2, 1, 1], [0, -7, -5, -2, -1]...

> M3b := addrow(M3a,1,3,-4);

M3b := matrix([[1, 1, 2, 1, 1], [0, -7, -5, -2, -1]...

> M3c := addrow(M3b,2,3,-1);

M3c := matrix([[1, 1, 2, 1, 1], [0, -7, -5, -2, -1]...

This completes Gaussian elimination on the system. If instead we want to perform Gauss-Jordan elimination, we continue on to the reduced row echelon form.

> M3d := mulrow(M3c,2,-1/7);

M3d := matrix([[1, 1, 2, 1, 1], [0, 1, 5/7, 2/7, 1/...

> M3e := addrow(M3d,2,1,-1);

M3e := matrix([[1, 0, 9/7, 5/7, 6/7], [0, 1, 5/7, 2...

Exercises:

4) Use elementary row operations on the matrix N1 from exercise 3 to obtain a row equivalent matrix in echelon form.

>

5) Use more row operations to reduce N1 to a row equivalent matrix in reduced echelon form.

>

Part3: Some more powerful linear algebra commands

Gaussian elimination and Gauss Jordan elimination are standard techniques in linear algebra. Rather than use row operations one by one, they can be performed with the gausselim and gaussjord commands.

> gausselim(M2);
gaussjord(M2);

matrix([[1, 1, 2, 1, 1], [0, -7, -5, -2, -1], [0, 0...

matrix([[1, 0, 9/7, 5/7, 6/7], [0, 1, 5/7, 2/7, 1/7...

Sometimes we do not need the reduced matrix, but only the number of nonzero rows in the reduced matrix. This is found using the rank command. The transpose of a matrix is given by the transpose command.

> rank(M2);
transpose(M2);

2

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

We started this section by converting systems of equations into matrix form. The command linsolve(MatCoef, ConstVec); finds the solution to a system which has MatCoef as the matrix of coefficients, and ConstVec the vector of constants.

> gensol := linsolve(M2a, M2b);

gensol := vector([1/2+5/2*_t[1]+1/2*_t[2], _t[1], _...

The _ti are Maple-provided parameters in the solution vectors. We can find a translation vector by setting the parameters to zero.

> transvec := subs(_t[1]=0, _t[2]=0, op(gensol));

transvec := vector([1/2, 0, 0, 1/2])

Spanning vectors are obtained by setting each parameter to 1 in turn and subtracting off the translation vectors.

> svec1 := subs(_t[1]=1, _t[2]=0, op(gensol));
spanvec1 := evalm(svec1- transvec);
svec2 := subs(_t[1]=0, _t[2]=1, op(gensol));
spanvec2 := evalm(svec2 - transvec);

svec1 := vector([3, 1, 0, -3])

spanvec1 := vector([5/2, 1, 0, -7/2])

svec2 := vector([1, 0, 1, -2])

spanvec2 := vector([1/2, 0, 1, -5/2])

Exercises:

6) Find the general solution to the system of equations you generated in exercise 3 above.

>

>

>