sm261_b5.mws

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

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

> with(LinearAlgebra);

Warning, the assigned name GramSchmidt now has a global binding

[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...

>

> solve({5*x+7*y=3,x+2*y=-1},{x,y});

{y = -8/3, x = 13/3}

> A := matrix( [[5,7],[1,2]] ):
b := vector( [3,-1] ):
linsolve(A, b);

vector([13/3, -8/3])

> B:=inverse(A);
evalm(B&*b);

B := matrix([[2/3, -7/3], [-1/3, 5/3]])

vector([13/3, -8/3])

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`);

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}

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...

> b := vector( [1,2,3] ):
linsolve(M1, b, 'rankM1');
rankM1;

vector([_t[2], _t[1], 2*_t[2]-5*_t[1]-1, -5*_t[2]+9...

2

> rref(M2);
solve(eqlist,varlist);

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

{y = 1/7-5/7*z-2/7*w, z = z, x = 6/7-9/7*z-5/7*w, w...

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);

A1 := matrix([[10, 7, -3, 3], [2, 3, 4, 3], [4, 2, ...

B0 := matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]])

B := matrix([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, ...

matrix([[0, 7, -3, 3], [2, -17, 4, 3], [4, 2, -25, ...

A1-10*B

matrix([[0, 7, -3, 3], [2, -17, 4, 3], [4, 2, -25, ...

>

> 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);

A2 := matrix([[7, 0, 3, -1, 0, 1, 3, 1, 5], [7, -3,...

matrix([[140, -12, 51, -10, 12, 3, 60, 10, 100], [5...

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

matrix([[7, 3, 1, 5], [7, 3, 0, 5], [0, 0, 4, 0], [...

matrix([[7, -3, 3, -1, 0, 5], [0, -3, 3, 4, 4, 0], ...

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

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

>

> rank(A2);
colspan(A2);
kernel(A2);
rref(A2);
gausselim(A2);

4

{vector([0, 0, 21, 0]), vector([0, 0, 0, 189]), vec...

{vector([0, 0, 1, 3/2, 0, 3, 0, -9/2, 0]), vector([...

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

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

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);

x*y*z+x*y+z*x+y*z

matrix([[0, z+1, y+1], [z+1, 0, x+1], [y+1, x+1, 0]...

[Maple Plot]

>

> det(H(x,y,z));
trace(H(x,y,z));

2*(z+1)*(y+1)*(x+1)

0

> 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);

-3.003119125, -1.761254658, 4.764373783

-3.003119125, -1.761254658, 4.764373783

false

false

A := matrix([[13, 6.3, 4.2], [6.3, 8.41, 6.], [4.2,...

3.102017972, 9.018724479, 22.69925755

true

true

> evalm(H(0,1,-1));
rank(H(0,1,-1));

matrix([[0, 0, 2], [0, 0, 1], [2, 1, 0]])

2

> 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);

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

lambda := -3, 3/2+1/2*sqrt(41), 3/2-1/2*sqrt(41)

-3.

4.701562118

-1.701562118

p := x^3-17*x-24

B := matrix([[0, 0, 24], [1, 0, 17], [0, 1, 0]])

-3, 3/2+1/2*sqrt(41), 3/2-1/2*sqrt(41)

-3., -1.701562119, 4.701562119

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);

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

1

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

2, 3, -1

C := matrix([[-7, -3, 3], [8, 5, -2], [-10, -3, 6]]...

-1, 2, 3

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);

true

matrix([[-2, -1, 1], [-1, 0, 1], [3, 1, -1]])

matrix([[-7, -3, 3], [8, 5, -2], [-10, -3, 6]])

matrix([[-7, -3, 3], [8, 5, -2], [-10, -3, 6]])

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);

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

1, 2, -6

true

matrix([[1, 0, 0], [0, -4, 2], [0, 6, 0]])

> ?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);

A := matrix([[-5, 2, -3, -3, -1], [-3, 3, 1, 1, 3],...

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);

-75

-3

matrix([[-7/25, -4/15, 47/75, 0, 4/75], [-3/25, -1/...

B := matrix([[-5, 2, -3, -3, -1, 1, 0, 0, 0, 0], [-...

matrix([[1, 0, 0, 0, 0, -7/25, -4/15, 47/75, 0, 4/7...

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);

poly := proc () `mod`(Randpoly(4,x),3) end proc

B := matrix([[2*x^4+x^2+2*x+1, 2*x^4+2*x^3+x, 2*x^4...

> rref(B);

matrix([[1, 0, 0, 0, (4*x^14+8*x^13+9*x^12+10*x^11-...

> colspan(B);
rank(B);

{vector([2*x^4+2*x^3+x, 2*x^4, 2*x^4+x^3+x+1, x^4+2...
{vector([2*x^4+2*x^3+x, 2*x^4, 2*x^4+x^3+x+1, x^4+2...
{vector([2*x^4+2*x^3+x, 2*x^4, 2*x^4+x^3+x+1, x^4+2...

4

Exercise : Do the same thing, but using random 3x4 matrices.