Linear algebra using MAPLE
"Board problem" set # 5, sm261, Prof Joyner
sm261_b5.mws,wdj,10-2002
>
> with(linalg);
Warning, the protected names norm and trace have been redefined and unprotected
> with(LinearAlgebra);
Warning, the assigned name GramSchmidt now has a global binding
>
> solve({5*x+7*y=3,x+2*y=-1},{x,y});
>
A := matrix( [[5,7],[1,2]] ):
b := vector( [3,-1] ):
linsolve(A, b);
>
B:=inverse(A);
evalm(B&*b);
Exercise : Solve x+y+z=1, x-y-z=1, x+y-z=0 in three ways: (1) using solve , (2) using linsolve , (3) using inverse .
>
>
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};
M1 := genmatrix(eqlist, varlist);
M2 := genmatrix(eqlist, varlist,`flag`);
>
b := vector( [1,2,3] ):
linsolve(M1, b, 'rankM1');
rankM1;
>
rref(M2);
solve(eqlist,varlist);
Exercise : Use Maple to convert the system{x-y+2z-2w=1, 2x+y+3w=4, 2x+3y+2z=6} to an augmented matrix. Solve it using (1) rref, (2) linsolve , and (3) solve .
>
>
A1 := matrix(3,4, [10,7,-3,3,2,3,4,3,4,2,5,1]);
B0 := diag(1,2,3);
B:=augment(B0,[0,0,0]);
matadd(A1, B, 1, -10);
A1-10*B;
evalm(A1-10*B);
>
>
A2 := matrix([[7,0,3,-1,0,1,3,1,5],[7,-3,3,0,3,-1,3,0,5],[0,-3,3,2,3,4,0,4,0],[7,0,3,2,0,4,3,4,5]]);
evalm(A1&*A2);
delrows(A2,2..3);
delcols(A2,2..6);
submatrix(A2,2..4,[1,2,3,6,8,9]);
swaprow(A2,1,2);
swapcol(A2,1,2);
>
>
rank(A2);
colspan(A2);
kernel(A2);
rref(A2);
gausselim(A2);
Exercise : Remove the 3rd and 7th columns from A2 and call the result B. Find (1) a basis for the kernel, (2) row-reduced echelon form, (3) a basis for the row span of B.
>
>
f:=(x,y,z)->x*y*z+x*y+x*z+y*z:
f(x,y,z);
H:=(a,b,c)->subs({x=a,y=b,z=c},hessian(f(x,y,z),[x,y,z])):
H(x,y,z);
plots[implicitplot3d](f(x,y,z)=1,x=-2..2,y=-2..2,z=-2..2,axes=boxed,scaling=constrained,style=patchcontour);
>
>
det(H(x,y,z));
trace(H(x,y,z));
>
eigenvalues(H(1.1,2,1));
eigenvalues(H(1.1,2,1));
definite(H(1.1,2,1),positive_semidef);
definite(H(1.1,2,1),positive_def);
A:=evalm(H(1.1,2,1)^2);
eigenvalues(A);
definite(A,positive_semidef);
definite(A,positive_def);
>
evalm(H(0,1,-1));
rank(H(0,1,-1));
>
A:=H(1,2,1);
lambda:=eigenvalues(H(1,2,1));
evalf(lambda[1]);
evalf(lambda[2]);
evalf(lambda[3]);
p:=charpoly(A,x);
B:=companion(p,x);
eigenvalues(B);
fsolve(p=0,x);
Exercise : Do the same thing for the function f(x,y,z)=(x-1)^4+y^3+z^2-1.
>
>
A:=matrix([[-1, 0, 1], [2, 1, -1], [-1, 1, 1]]);
det(A);
D0 := diag(2,3,-1);
eigenvalues(D0);
C:=evalm(A&*D0&*A^(-1));
eigenvalues(C);
Exercise : Find the eigenvalues of C using (1) the eigenvectors command and (2) charpoly and fsolve command.
>
>
issimilar(C,D0,`P`);
evalm(P);
evalm(P^(-1)&*D0&*P);
evalm(C);
Exercise
: The matrix A=[[1,0,0],[0,-4,2],[0,6,0]] has eigenvalues 1, 2, -6. As a consequence of a theorem in linear algebra (we will get to later in
the semester), there is a matrix P such that diag(1,2,-6)=P^(-1)*A*P. (We say diag(1,2,-6) and A are
conjugate
or
similar
.) Find P using
issimilar
.
>
A:=matrix([[1,0,0],[0,-4,2],[0,6,0]]);
eigenvalues(A);
issimilar(A,diag(1,2,-6),`P2`);
evalm(P2^(-1)&*diag(1,2,-6)&*P2);
> ?randmatrix
Here is a random 5x5 matrix:
>
f:=(x,y)->(rand() mod 10)-6:
a:=[seq([seq(f(i,j),i=1..5)],j=1..5)]:
A:=matrix(a);
We compute its trace, determinant, and its inverse in 2 ways:
>
det(A);
trace(A);
evalm(A^(-1));
B:=augment(A,diag(1,1,1,1,1));
rref(B);
Exercise : Do the same thing, but using random 4x4 matrices.
>
>
poly := proc() Randpoly(4, x) mod 3 end proc;
B:=randmatrix(4,5,entries=poly);
> rref(B);
>
colspan(B);
rank(B);
Exercise : Do the same thing, but using random 3x4 matrices.