











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to using maple for matrix operations and solving linear equations. It covers topics such as entering matrices, row reduction, and solving systems of linear equations. The document also discusses the difference between vectors and lists in maple and how to keep track of coefficients during row reduction.
Typology: Lab Reports
1 / 19
This page cannot be seen from the preview
Don't miss anything!












Press the Ctrl-Alt-Delete keys simultaneously. You should get a
login prompt. Enter your ORST user name and press the Tab key.
Then enter your ORST password and press the Enter key.
Make sure you save your work on the Z: drive. This drive is actually
your personal space on the lab server and will be available if you log
in later on a different workstation.
To start Maple (or Matlab, or Mathematica, ... ) select the Start
button (lower left corner of the screen), then Programs from the
menu, etc. Alternately, if you use the browser, Internet Explorer or
Netscape, to download a Maple Worksheet it will offer to start
Maple for you.
When you are done with your session, you must logout. One way to
do it is to press Ctrl-Alt-Delete. A menu will appear. Select Logoff.
If you do not logout you leave your account open for the next
person to come along. That person will have access to your personal
files on the ORST server. Do not forget to logout, even if you are
just leaving for a short while!
In the live version of this worksheet I have removed all of the Maple
output. If you press the Enter key over each Maple command below
(in order) you will see Maple's responses appear. Fell free to modify
anything as you go along!
Sometimes it is desirable to be able to restart more or less cleanly. If
we begin the worksheet with a restart command, then we can just
re-execute the whole worksheet in order to clean up a mess and do
everything in sequential order. This is a useful trick, but it is not a
good idea if your worksheet contains some very length calculations
(which you do not want to redo).
> restart;
We will be doing some linear algebra, so we load the linear algebra
library, linalg. This library defines some of the commands and data
structures that we will use. When we load a library it announces all
of the commands that it defines. We can suppress this output by
using a colon, in place of the usual command-terminanting
semicolon. Here I used a semicolon so you can see the list of
commands defined by the linalg library.
> with(linalg);
Warning, new definition for norm
Warning, new definition for trace
> A:=matrix(3,4,[1,2,3,4,0,-1,3,4,2,-1,2,3]);
Personally I prefer the second method for interactive entry. It is also
possible to build a matrix by specifying its columns (as vectors or
lists). Thus
> a[1]:=[1,0,2]; a[2]:=[2,-1,-1];
a[3]:=[3,3,2]; a[4]:=[4,4,3];
a 1 :=[ 1 0 2, , ]
a := 2
a 3 :=[ 3 3 2, , ]
a 4 :=[ 4 4 3, , ]
Now we use the augment command to build the matrix:
> A:=augment(a[1],a[2],a[3],a[4]);
Alternately we can build the matrix by entering its rows and then
stacking them on top of each other with the stackmatrix command.
> b[1]:=[1,2,3,4]; b[2]:=[0,-1,3,4];
b[3]:=[2,-1,2,3];
b 1 :=[ 1 2 3 4, , , ]
b 2 :=[ 0 -1 3 4, , , ]
b 3 :=[ 2 -1 2 3, , , ]
> A:=stackmatrix(b[1],b[2],b[3]);
There is also a command called entermatrix which may be used to
build a matrix. It is a special purpose command though and not very
convenient unless you enjoy typing numerous semicolons.
Diagonal matrices can be entered in a natural way.
> diag(1,2,3,4);
The diag command gives you a simple way to define your own n by
n identity matrix, IE(n):
followed by a semicolon.
As we see, Maple makes an exception for matrices. The reason is
they can be quite large and you may not wish to waste time
displaying them, nor to clutter up your worksheet. You can always
use the evalm function to force matrix evaluation
> evalm(A);
Now suppose we want to row reduce A ourselves. We'd want to
add -2 times the first row to the third row:
> A1:=addrow(A,1,3,-2);
Note I assigned the result to A1 so I would be able to refer to it in
the next step. An alternative approach is to use the ditto operator,
%, but it is risky to use except within the same line, because it refers
to the previous expression evaluated, which may not be the previous
expression on your worksheet if you have done a lot of editing.
Here's an example of using the % operator. I stack up two
commands on one line, so I will not have to worry what % points to.
> addrow(A1,2,1,2); A2:=addrow(%,2,3,-5);
> mulrow(A2,2,-1); A3:=mulrow(%,3,-1/19);
> addrow(A3,3,1,-9); A4:=addrow(%,3,2,3);
> soln:=linsolve(A,b,rnk,s);
soln :=
s 1 , − − , ,
s 1 +
s 1
s 1
Note the parameter name has to appear as the fourth variable, so we
had to insert a third variable, rnk. It turns out that linsolve stuffs the
rank of A into the third variable. Thus
> rnk;
One can pick out the individual components easily
> soln[1]; soln[2]; soln[3]; soln[4];
s 1
s 1
s 1
s 1
We can also solve directly by row reduction of course -
> M:=augment(A,b); R:=rref(M);
Here we can read the solution easily. Moreover, we get the
canonical form of the solution, with the free variable as the
parameter. This is not the form that Maple returned above (though it
is equivalent, of course).
> for k from 1 to 3 do x[k]:=R[k,5]-R[k,4]s;*
od; x[4]:=s;
x 1 :=− −
s
x 2 :=− +
s
x 3 := −
s
x := 4
s
For more complicated situations this could get out of hand and
First we form the matrix with columns u, v and w. Then we solve
the system of linear equations with this matrix as the coefficient
matrix and with b as the inhomogeneous term.
> B:=augment(u,v,w); linsolve(B,b);
We see b = -3u + 6v -2w. What happens if there is no solution?
Here's an example:
> C:=matrix(2,2,[1,1,1,1]); b:=[1,2];
b :=[ 1 2, ]
> linsolve(C,b);
> rref(augment(C,b));
There is no solution and therefore Maple returns no response.
We have discussed matrix multiplicatio a little bit in class already.
In Maple the notation for matrix multiplication is &*. It looks
strange, but the ampersand warns Maple's symbolic simplification
routines that this multiplication is non-commutative.
Here's a few examples:
> A:=randmatrix(3,3); B:=randmatrix(3,3);
> evalm(A & B); evalm(B & A);**
Notice I had to force Maple to evaluate these expressions. The
default behavior is to return the product unevaluated. This is very
useful behavior in some cases where intermediate results are
inconveniently large or not actually needed.
Consider now three vectors:
> c1:=vector([1,2,3]); c2:=vector([0,1,4]);
c3:=vector([1,3,7]);
c1 :=[ 1 2 3, , ]
That's strange. This result implies no solutions at all, yet there
certainly is a solution if b = 0 for example. The problem is that
Maple simplifies expressions such as z/z to 1 when z is symbolic.
Thus Maple's answer is actually correct in the sense that there is no
solution which is a rational expression in b1, b2 and b3.
One way out of the impasse is to note tht we can keep track of the
coefficients of b1, b2 and b3 during row reduction by keeping them
in separate columns. Thus we augment C by an identity matrix and
row reduce.
> augment(C,IE(3)); R:=rref(%);
There is no pivot in the first 3 columns in the last row, so the
remaining part of the last row yields a compatability condition on b
> evalm(submatrix(R,3..3,4..6) & b) = 0;*
b1 − +
b
b3 0
Now we can read of the solutions easily. At this point you know
enough about Maple to check your answers to all of the homework
problems for the material covered so far in class. Try it out!
One comment - Maple commands are polymorphic. Their behavior
depends on the number and type of parameters that you feed to
them. Thus even though lists and vectors are different data types in
Maple, the linear algebra routines for the most part do not care
which you use.
> a:=[1,2,3]; b:=vector([1,2,3]);
c:=matrix(1,3,[1,2,3]);
a :=[ 1 2 3, , ]
b :=[ 1 2 3, , ]
c :=[ 1 2 3 ]
> type(a,vector); type(a,list); type(a, array);
type(a,matrix);
false
true
false
false
> type(b,vector); type(b,list); type(b,array);
Ouch! Our 2 by 2 matrix became a scalar when we multiplied by 0.
Some very strange error messages could result later! Here the
solution is to use the scalar multiplication function
> scalarmul(N,0); scalarmul(N,3);
Of course, it is rare for people to be so careful all the time. The key
is to think about your results, to understand what is going on and to
add the right level of precision to guarantee your results. Even with
computer algebra systems there is no substitute for understanding.
Have fun with Maple!