%%%%%%%%%%%%%%%%%%%% QUIZ 9 Solutions in MATLAB 16 points %%%%%%%%%%%%%%%%% % Problem 1 (2 pts) A = -1 1 2 0 4 0 2 -1 0 0 1 0 -1 13 4 2 %%% % 1a (1/2 pt) >> det(A) ans = -20 % Therefore, since det(A) is not 0, A is invertible %%% % 1b (1/2 pt) >> inv(A) ans = -0.6500 0.1000 0.9000 0.0500 0.3500 0.1000 -1.1000 0.0500 0 0 1.0000 0 -2.6000 -0.6000 5.6000 0.2000 %%% % 1c (1/2 pt), solve the eqn Ax = b using the inverse of A >> inv(A)*[1 2 0 1]' ans = -0.4000 0.6000 0 -3.6000 %%% % 1d (1/2 pt), now solve eqn using GE >> A\[1 2 0 1]' ans = -0.4000 0.6000 -0.0000 -3.6000 % Both approaches in 1c and 1d give the same result. This always occurs when A is invertible, so there is % only a single solution to Ax = b, and so all methods, including least squares, give the same solution. %%%%%%%%%%%%%%%%%%%%%%% % Problem 2 (1.5 pts) >> C=[-7 -9 -4 5 3 -3 -7; -4 6 7 -2 -6 -5 5; 5 -7 -6 5 -6 2 8; -3 5 8 -1 -7 -4 8; 6 -8 -5 4 4 9 3] C = -7 -9 -4 5 3 -3 -7 -4 6 7 -2 -6 -5 5 5 -7 -6 5 -6 2 8 -3 5 8 -1 -7 -4 8 6 -8 -5 4 4 9 3 >> b=ones(5,1) b = 1 1 1 1 1 %%% % 2a (1/2 pt) >> x_matlab=C\b ans = -0.9286 0.4660 -0.8781 0 0.7273 0 1.0000 %%% % 2b (1 pt) >> norm(C*x_matlab - b) ans = 6.9935e-015 % Since the norm of the residual is small (and on the order % of machine precision, then this is a solution to Cx = b. % But I hope you wonder if there is only one solution (and this is it) % or if there are an infinite number of solutions. % The answer to this question is found in the next problem. %%%%%%%%%%%%%%%%%%%%% % % Problem 3 (3 pts) % %%% % 3a (1/2 pt) >> null(C) ans = 0.0000 0.0000 0.5242 0.1715 -0.0346 -0.2672 0.8207 0.1015 -0.0481 0.6408 0.1550 -0.4890 -0.1550 0.4890 % Thus, the nullspace N(C) is two dimensional, with basis vectors given by the columns % of the matrix returned by MATLAB % from the null(C) command %%% % 3b (1/2 pt) >> rref(C) ans = 1.0000 0 0 0 0 0 0 0 1.0000 0 0 -3.7308 0 4.5385 0 0 1.0000 0 0.7692 0 -0.4615 0 0 0 1.0000 -5.5000 0 7.0000 0 0 0 0 0 1.0000 1.0000 % Thus, the column space is 5 dimensional, with % the 1st, 2nd, 3rd, 4th and 6th columns forming a basis of the % column space. % % The next command verifies that the column space has dimension 5 >> rank(C) ans = 5 %%% % 3c (1/2 pt) % To answer this question, first remind myself that there are 5 rows and 7 columns: >> size(C) ans = 5 7 % So the Dimension Theorem states that dim of row space and dim of column space (= rank(A)) are the same at 5. % Furthermore, the column space is all of Re^5 and so dim(N(A')) = 0. On the other hand, the row space is only a % 5-D subspace of the domain = Re^7, % which makes sense since we showed in 3a that dim(N(C)) = 2. %%% % 3d (1 pt) % By the Fredholm Alternative, since the column space is all of Re^5 but there is a null space, then there % are always an infinite number of solutions to Cx = b for any RHS b. The infinite number of solutions look like % % x_p + x_h = x_matlab + null(C)*y % % where y is a 2x1 vector composed of any two real numbers %%%%%%%%%%%%%%%%%% % % Problem 4 (6 pts) %%% % 4a >> Age = [19 81 31 49] Age = 19 81 31 49 >> Income = [18 21 52 61]' Income = 18 21 52 61 >> plot(Age,Income,'.') >> xlabel('Age (years)') >> ylabel('Income (thousands of dollars') >> title('Cash flow of my four friends') %%% % 4b (1 pt) >> A=[ones(1,4); Age; Age.^2]' A = 1 19 361 1 81 6561 1 31 961 1 49 2401 % So Ax = b is the same as A*[a b c]' = Income %%% % 4c (1 pt) >> rref([A Income]) ans = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 % The last line says that 0 = 1, an impossibility, % so there are no solutions to this system! %%% % 4d (1/2 pt) % You were told that this is the way to use GE: >> x_matlab = A \ Income ans = -49.6201 4.5358 -0.0453 % But we found in 4c that there are no solutions, what is MATLAB doing? %%% % 4e (1/2 pt) >> norm(A*x_matlab - Income) ans = 5.8248 % Therefore, x_matlab is not the solution to the system! %%% % 4f (1 pt) >> rref(A) ans = 1 0 0 0 1 0 0 0 1 0 0 0 % This shows that each column has a pivot. % Therefore the columns of A are linearly independent, and so we expect a single LS solution. %%% % 4g (1/2 pt) >> xls = (A'*A)\(A'*Income) xls = -49.6201 4.5358 -0.0453 %%% % 4h (1/2 pt) % x_matlab and xls are the same. In other words, A\Income gave the least squares solution % instead of reporting that there were no solutions. %%% % 4i (1 pt) figure(1) hold on ax = axis % Gets the size of the current graphing window agegrid = ax(1):.1:ax(2); quad = xls(1) + xls(2)*agegrid + xls(3)*agegrid.^2; plot(xgrid,quad,'r') title('Quadratic fit to the cash flow of my four friends') %%%%%%%%%%%%%%%%%%%% % % Problem 5 (3.5 pts) >> F=[-7 -9 -4 5 3; -4 6 7 -2 -6; 5 -7 -6 5 -6; -3 5 8 -1 -7; 6 -8 -5 4 4] F = -7 -9 -4 5 3 -4 6 7 -2 -6 5 -7 -6 5 -6 -3 5 8 -1 -7 6 -8 -5 4 4 >> B=F'*F B = 135 -59 -84 25 18 -59 255 200 -129 -88 -84 200 190 -92 -94 25 -129 -92 71 20 18 -88 -94 20 146 % 5a (1/2 pt) %e-pairs >> [S Lambda]=eig(B) S = 0.0000 0.2864 -0.5165 0.7707 0.2391 -0.5515 -0.3411 -0.3806 0.0749 -0.6549 0.1137 0.8046 0.0872 -0.0629 -0.5728 -0.8131 0.2973 0.3895 0.0544 0.3096 -0.1478 0.2563 -0.6550 -0.6273 0.2999 Lambda = 0.0000 0 0 0 0 0 11.3620 0 0 0 0 0 109.6797 0 0 0 0 0 123.2346 0 0 0 0 0 552.7237 % The columns of S are the eigenvectos, the diagonal of Lambda contains the corresponding % eigenvalues %%% % 5b (1/2 pt) >> S\B*S ans = 0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 11.3620 -0.0000 -0.0000 -0.0000 0.0000 -0.0000 109.6797 0.0000 -0.0000 0.0000 -0.0000 0.0000 123.2346 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 552.7237 %This is the diagonal matrix Lambda with eigenvalues on the diagonal. This is to be expected, since % B has n linearly independent eigenvectors, and hence is diagonalizable, i.e., B = S*Lambda*inv(S). %%% % 5c (1 pt) % From 5a, B has a zero eigenvalue. Therefore, det(B)=0, and so it is not invertible. %%% %5d (1/2 pt) % The single basis vector for N(B) is >> null(B) ans = 0 0.5515 -0.1137 0.8131 0.1478 %%% %5e (1/2 pt) % The vector in 5d is the same as the eigenvector for the 0-eigenvalue we found for B. %%% %5f (1/2 pt) % By definition, eigenvectors with 0 eigenvalues are always in the nullspace of B