You are on page 1of 3

Engineering Mathematics 2

Laboratory Session 1
Learning outcomes for this session
At the end of this session, you will be able to
1. Define matrices and vectors in Maple.
2. Perform simple arithmetic operations on vectors and matrices in Maple.
3. Use Maple to perform elementary row operation on a matrix.
Overview
1. Notation for matrices and vectors and their arithmetic.
2. Maple commands introduced in this worksheet: Array, Matrix, Column, Vector.
Important Note: To be consistent across the various laboratories, you should be using the
Classic Worksheet version of Maple. Even if the standard version of Maple is available, the
discussion below may be misleading or confusing if it is used instead of the classic worksheet
version.
1.

Maple comes with a linear algebra package that will allow you to manipulate vectors and
matrices. To load the package, use the command with(LinearAlgebra):
Note the spelling of the name of the package. You can end the command with a semi-colon if you
want to see the new routines that become known to Maple once the package has been loaded.
After you have loaded the package, type in the following commands:
[> A:=Array(1..3,1..4);
[> B:=Array([[1,2,3,4],[4,3,2,1],[1,2,1,2]]);
[> C:=Matrix([[1,2,3,4],[4,3,2,1],[1,2,1,2]]);
What is the difference between B and C? Try the commands
[> type(B, Matrix);
[> type(C, Matrix);
A vector is a column matrix and the vector with components a, b, and c, can be entered as
[> v:= Vector([a,b,c]);
Maple treats Vector as a column matrix.
Maple distinguishes between an Array object and a Matrix object. Although both look the same
on the screen, they are different objects in Maples internal representation. Note carefully the
syntax for both an Array and a Matrix object. The array or matrix entries can be accessed using
the standard suffix notation. So to refer to the (i,j)-th entry in the matrix B, we simple type
B[i, j]. Suffixes are always inside square brackets. Because Maple ignores spaces and carriage
returns, we can use the spacing to our advantage, e.g.
> P:=Matrix([[1,2,3,4],
>
[5,6,7,8],
>
[9,10,11,12]]);

2.

If a matrix has a nice structure, then its creation is usually simpler. Copy the following
commands onto your worksheet and note what matrix is created in each case.
> M1:=Matrix(8,8, (i,j)->i^2/(i+j));
> di:=Matrix(1..3,1..3,Vector([1,2,3]),shape=diagonal);
> bb:=BandMatrix([1,-4,1],1,5,5);
> I1:=Matrix(5,5,shape=identity);

3.

1
i

j . Call your matrix M.


(a) Create a 6 6 matrix whose ijth entry is
[You should be able to do this with a single command.]

4.

(b) The function Column(M, k) will extract the k-th column of the matrix M. Store the 4-th
column of M in the name U and then check whether U is a Vector object.
[Watch the spelling. If you typed vector instead of Vector, you will get the wrong answer.]
Multiplying 2 matrices is done by using the . symbol. Using the matrix P from question 1 and
the matrix S defined by S:=Matrix(4,5,(i,j)->i+j);, the product is obtained by the
command
[> P . S;
Since P is a 3 4 matrix and S is 4 5, the product is defined. Note that the product S . P is not
defined. Now try to type in
[> P*S;
You will get an error message. Maple makes a difference between operations that are order
dependent and those that are not dependent on order. Because matrix products are not
commutative in general, we use the dot operator instead of the * operator, which is used when
the operation is not dependent on order. For example, 2*3 is the same as 3*2 and between any
two scalars, the * is used. For any two matrices, A, B say, AB and BA are, in general, not the
same, and hence for matrix multiplication, we must use the dot . symbol.
Verify this for each of the following pairs of matrices.
1 2 1

2 0 1

3 1 0

4 1 1

(a) A:=Matrix([[1,2,1],[2,0,1],[3,1,0],[4,1,1]]);
B:=Matrix([[1,2,1,-1],

[1,1,0,1],[1,2,1,1]]);

B 1

2
1
2

(b) A:=Matrix([[a,c],[b,d]]);

1
0
1

-1

1
a
b

B:=Matrix([[x,z],[y,w]]);

x
y

On the other hand, when multiplying by a scalar, we must use the * symbol, since kA and
Ak are always the same when k is any scalar. Now verify that 2*A works but 2 . A does not.
5.

Shortcuts for entering matrices and vectors. Maple uses angle brackets to denote vectors, thus the
vector v of question 1 is represented by <a,b,c>. Try the command
[> v:=<a,b,c>;
A matrix may be interpreted as being composed of column vectors. Thus the matrix A of question
4 can be entered as
[> A := <<1,2,3,4>|<2,0,1,1>|<1,1,0,1>>;
Note that values separated by commas inside angle brackets are interpreted as column entries of
a vector. The vertical strut, | separates the columns of the matrix. All the columns are enclosed
within angle brackets also.
Now, use the shorthand notation to enter the following matrices and vectors:

v 1

(a)

7 ; v1:=<1,2,3,5,7>;

v2 y

z ;
(b)

(c)

b
A
c

B -1

2
(d)

v2:=<x,y,z>;
e
f
g
h

1
1
1
0
1

l ; A:=<<a,b,c,d>|<e,f,g,h>|<i,j,k,l>>;
0
1
0
1
0

3
1
3
0
2

c B:=<<2,1,-1,0,2>|<1,1,1,0,1>|<0,1,0,1,0>|<3,1,3,0,2>|<2,1,a,b,c>>

(Use v[1] and v[2] to indicate the subscripts.)


6.

Calculate the product Bv1 for the objects defined in the previous question.
B.v1

7.

Look up the RowOperation command and use it to reduce the matrix A of question 4(a) to row
echelon form. Ask your tutor about row echelon forms and elementary row reduction if you
havent met these concepts or if youve forgotten their meaning.

A:=<<1,2,3,4>|<2,0,1,1>|<1,1,0,1>>;
RowOperation(A,[2,1],-2);
RowOperation(%,[3,1],-3);
RowOperation(%,[4,1],-4);
RowOperation(%,[3,2],-5/4);
RowOperation(%,[4,2],-7/4);

You might also like