> # MA305 Homework #4 KEY > restart; > with(linalg): > grade:=0; Warning, the protected names norm and trace have been redefined and unprotected grade := 0 > #1 > A1:=matrix([[1,-3,4,-2],[-2,0,1,3],[4,6,-5,0]]); [ 1 -3 4 -2] [ ] A1 := [-2 0 1 3] [ ] [ 4 6 -5 0] > #a. Find a basis for the column space of A1. > gausselim(A1); [1 -3 4 -2] [ ] [0 -6 9 -1] [ ] [0 0 6 5] > # The REF indicates that the first three columns of A1 are linearly > independant. > basis_colspace_A1:={col(A1,1..3)}; basis_colspace_A1 := {[1, -2, 4], [-3, 0, 6], [4, 1, -5]} > grade:=grade+2; grade := 2 > #b Find a basis for the rowspace of A1. > # Since the row and column spaces always have the same dimension, we > could just say, the three rows of A are independant, hence are a basis > for the row space. > # Or we can do what we did before: > gausselim(transpose(A1)); [1 -2 4] [ ] [0 -6 18] [ ] [0 0 6] [ ] [0 0 0] > # The RREF indicates that the first three columns of the A1^T are > linearly independant, hence the first three rows of A1 are linear > independant. > basis_rowspace_A1 := {row(A1,1..3)}; basis_rowspace_A1 := {[-2, 0, 1, 3], [1, -3, 4, -2], [4, 6, -5, 0]} > grade:=grade+2; grade := 4 > #c What is the Rank of A1? > > # You shouldn't need to use the Rank command here. > #The Rank is the dimension of the column Space, which we know from (a) > is 3. > rank(A1); 3 > grade:=grade+2; grade := 6 > #d True or False. For any b in R^3, there is a solution to Ax=b? > > #TRUE > > #The statement Ax=b also means that b can be written as a linear > combination of the columns of A. Since the column space of A is all > of R^3, every b can be written as a linear combination of the columns > of A. > > grade:=grade+2; > grade := 8 > #e True or False. For any b in R^3, there is at most one solution to > Ax=b? > > #FALSE. > # There are four columns of A, but the column space has dimension > three. So there is some non-zero linear combination of the four > columns which is 0. If we assemble the coefficients of such a linear > combination into a vector v, then Av=0. So for any b, there are > infinitely many solutions: A.x=b (guaranteed to exist form part d), > and A.(x+av)=b (x with any multiple of v added to it). > > grade:=grade+2; > grade := 10 > #2 > u2:=vector([1,-1,2,0]); v2:=vector([2,0,-1,-2]); > w2:=vector([-1,3,8,4]); u2 := [1, -1, 2, 0] v2 := [2, 0, -1, -2] w2 := [-1, 3, 8, 4] > #a. Compute the scalar product: u.v, u.w, v.w > dotprod(u2,v2); 0 > dotprod(u2,w2); 12 > dotprod(v2,w2); -18 > > grade:=grade+3; > grade := 13 > #b. Which pairs are orthogonal. > # Two vectors are orthogonal if their dot product is 0. Hence: > #u and v are orthogonal > #u and w are not > #v and w are not > > grade:=grade+3; > grade := 16 > #c. Compute the Euclidean Norms: ||v||, ||w||, ||u-v|| > norm(v2,2); 3 > norm(w2,2); 3 sqrt(10) > norm(u2-v2,2); sqrt(15) > > grade:=grade+3; > grade := 19 > #d. The angle between v and w. > > # If you read the Maple docs: > angle(v2,w2); Pi - arccos(1/45 sqrt(9) sqrt(90)) > # No one really cares what this is in degrees: > 180/Pi * angle(v2,w2); > evalf(%); Pi - arccos(1/45 sqrt(9) sqrt(90)) 180 ---------------------------------- Pi 129.2315205 > > > # If you actually knew what you were doing: > arccos(dotprod(v2,w2)/(norm(v2,2)*norm(w2,2))); Pi - arccos(1/5 sqrt(10)) > > grade:=grade+2; > grade := 21 > #e. The projection of w onto v. > > proj[v](w):=evalm(dotprod(v2,w2)/(dotprod(v2,v2))*v2); proj[v](w) := [-4, 0, 2, 4] > grade:=grade+2; > grade := 23 > #f. The orthogonal complement of span(u,v,w) > > # Assemble the vectors u, v, and w into the rows of a matrix A. If a > vector x is orthogonal to each of these vectors, then A.x = [u.x, v.x, > w.x]^T = 0 > # So to find a basis for the orthogonal complement, we need only find > a basis for the nullspace of A (i.e. all the solutions to A.x = 0): > > x2:=op(nullspace(matrix([u2,v2,w2]))); x2 := [-2, 0, 1, -5/2] > # The orthogonal complement of Span(u2,v2,w2) is Span(x2) > # Try it: > dotprod(u2,x2);dotprod(v2,x2);dotprod(w2,x2); 0 0 0 > grade:=grade+3; grade := 26 > > #3. > v3:=vector([1,0,-1,0,3]); > w3:=vector([-1,0,2,-1,0]); > z3:=vector([4,-3,1,0,0]); > b3:=vector([5,7,-4,3,-2]); v3 := [1, 0, -1, 0, 3] w3 := [-1, 0, 2, -1, 0] z3 := [4, -3, 1, 0, 0] b3 := [5, 7, -4, 3, -2] > > #a. Compute b_hat, the orthogonal projection of b onto Span(v,w,z) > > # Least squares is your friend. We'll put v, w, and z into a matrix A > as columns. Then we will use least squares to find the b_hat closest > to b, so that there is a solution to Ax=b_hat > A3 := transpose(matrix([v3,w3,z3])); [ 1 -1 4] [ ] [ 0 0 -3] [ ] A3 := [-1 2 1] [ ] [ 0 -1 0] [ ] [ 3 0 0] > # If you read the Maple docs: > x_hat:=leastsqrs(A3,b3); > b_hat:=evalm(A3&*x_hat); [-159 -4281 -529] x_hat := [----, -----, ----] [355 1420 1420] [1529 1587 -1691 4281 -477] b_hat := [----, ----, -----, ----, ----] [1420 1420 284 1420 355 ] > #Or if you actually knew how to do this: > x_hat:=linsolve(transpose(A3)&*A3,transpose(A3)&*b3); > b_hat:=evalm(A3&*x_hat); [-159 -4281 -529] x_hat := [----, -----, ----] [355 1420 1420] [1529 1587 -1691 4281 -477] b_hat := [----, ----, -----, ----, ----] [1420 1420 284 1420 355 ] > grade:=grade+3; grade := 29 > > #b. Compute the infinity norm and the Manhatten norm of b-b_hat > # If you read the Maple docs: > norm(b3-b_hat,1); # Manhatten norm, aka the 1-norm 4413 ---- 355 > norm(b3-b_hat,infinity); 8353 ---- 1420 > # Also, do able by direct manipulation of the vectors: > > c:=convert(evalm(b3-b_hat),list); # Convert b-b_hat to a list, makes > life easier > 5571 8353 555 -21 -233 c := [----, ----, ---, ----, ----] 1420 1420 284 1420 355 > c:=map(abs,c); # Take the absolute value of each element 5571 8353 555 21 233 c := [----, ----, ---, ----, ---] 1420 1420 284 1420 355 > # Infinity Norm: Just take the max > max(op(c)); 8353 ---- 1420 > # Manhatten Norm: Just find the sum > convert(c,`+`); 4413 ---- 355 > grade:=grade+4; grade := 33 > > > > > >