%% Quiz 10 Solutions %% Exercise 24, p. 409 % clear; %It's a good idea to make each piece self-contained A=[-10 13 7 -11; 2 1 -5 3; -6 3 13 -3; 16 -16 -2 5; 2 1 -5 -7]; % For the solution, fill in the columns of a matrix V. I'll get you % started: V(:,1)=A(:,1); V(:,1)=V(:,1)/sqrt(V(:,1)'*V(:,1)); V(:,2)=A(:,2)-(A(:,2)'*V(:,1))*V(:,1); V(:,2)=V(:,2)/sqrt(V(:,2)'*V(:,2)); V(:,3)=A(:,3)-(A(:,3)'*V(:,1))*V(:,1)-(A(:,3)'*V(:,2))*V(:,2); V(:,3)=V(:,3)/sqrt(V(:,3)'*V(:,3)); V(:,4)=A(:,4)-(A(:,4)'*V(:,1))*V(:,1)-(A(:,4)'*V(:,2))*V(:,2)-(A(:,4)'*V(:,3))*V(:,3); V(:,4)=V(:,4)/sqrt(V(:,4)'*V(:,4)); %Check- This should be the 4 x 4 identity matrix V'*V %% Exercise 25, p. 409: Use your previous code to create QR clear A=[-10 13 7 -11; 2 1 -5 3; -6 3 13 -3; 16 -16 -2 5; 2 1 -5 -7]; V(:,1)=A(:,1); V(:,1)=V(:,1)/sqrt(V(:,1)'*V(:,1)); V(:,2)=A(:,2)-(A(:,2)'*V(:,1))*V(:,1); V(:,2)=V(:,2)/sqrt(V(:,2)'*V(:,2)); V(:,3)=A(:,3)-(A(:,3)'*V(:,1))*V(:,1)-(A(:,3)'*V(:,2))*V(:,2); V(:,3)=V(:,3)/sqrt(V(:,3)'*V(:,3)); V(:,4)=A(:,4)-(A(:,4)'*V(:,1))*V(:,1)-(A(:,4)'*V(:,2))*V(:,2)-(A(:,4)'*V(:,3))*V(:,3); V(:,4)=V(:,4)/sqrt(V(:,4)'*V(:,4)); Q=V; R=Q'*A; %Check: Q*R %% Exercise 26 The problem uses a "for loop" below. clear A=[-10 13 7 -11; 2 1 -5 3; -6 3 13 -3; 16 -16 -2 5; 2 1 -5 -7]; % See if you can figure out what each line does. [nr,nc]=size(A); %computes the number of rows, cols of A q=A(:,1)/norm(A(:,1)); % initializes the first column of Q Q=q; for j=2:nc %Loop x=A(:,j); % Take the jth column of A v=x-Q*Q'*x; % Remove the components of x that are in Col(Q) v=v/norm(v); % Normalize v - A good idea! Q=[Q v]; % Add that column to the rest. end %End the loop R=Q'*A; % Compute R clear ans j nc nr q v x Q R %% Hanford Data load HanfordData1 % We have 9 data points- Index (the indep variable) and Deaths (dependent) x=Index; y=Deaths; A=[x(:), ones(9,1)]; % Find the slope and intercept, and label then as m, b B=inv(A'*A)*A'*y'; m=B(1) b=B(2) % Plotting routine (using the m, b found previously) figure(1) xx=linspace(min(x),max(x),200); yy= m*xx + b; plot(x,y,'b^',xx,yy,'k-'); %See the help file for plot to see what this means %% Data Set 2: Knee Girth to Height % Download the data set Body1.mat from the class website clear; %Clear the old variables out load Body1 % Some plotting commands to see the data first: figure(2) plot(KneeGirth,Height,'.') plot(KneeGirth(menidx),Height(menidx),'r.',KneeGirth(womenidx),Height(womenidx),'b.'); legend('Men','Women') x=KneeGirth; y=Height; A=[KneeGirth,ones(size(KneeGirth))]; % Get the slope and intercept, and label them as m, b B=inv(A'*A)*A'*y; m=B(1) b=B(2) % Once m, b are found, you can run this code: xx=linspace(min(x),max(x),200); yy= m*xx + b; figure(3); %Opens a new figure for the new plot plot(x,y,'b.',xx,yy,'k-');