Project 4 Solutions

# Project 4 Solutions - Project 4 Group name Unknowns Group...

This preview shows pages 1–5. Sign up to view the full content.

Project 4 Group name: Unknowns Group members: Annon, Sofia, Hristian, Divya 1. Matlab code: function x=mgslsq(A,b) [Q,R]=mgs([A b]); R1=R(1:size(R,1)-1,1:size(R,2)-1); c=R(1:size(R,1)-1,size(R,2)); x=backsub(R1,c); a) >> A=hilb(6);A=A(:,1:4);b=A*ones(4,1); >> x=mgslsq(A,b) x = 0 0 0 0 >> norm(A*x-b) ans = 2.8578 >> x=A\b x = 1.0000 1.0000 1.0000 1.0000 >> norm(A*x-b) ans = 1.0991e-015 We can see that the second method is much better, it gives a much smaller residual. b) >> A=ones(6)+eps*eye(6);A=A(:,1:4);b=A*ones(4,1);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
>> x=mgslsq(A,b) x = 0 0 0 0 >> norm(A*x-b) ans = 9.7980 >> x=A\b Warning: Rank deficient, rank = 1, tol = 3.2634e-015. x = 4.0000 0 0 0 >> norm(A*x-b) ans = 1.9860e-015 The second method gives a smaller residual. --------------------------------------------------------------------------------------------------------------------------------------------------- ------------- 2. Matlab code: function [Q,R]=givensqr(A) [m,n]=size(A); R=A; Q=eye(m,m); for i=1:n for j=i+1:m r= norm([R(i,i);R(j,i)]); c=R(i,i)/r; s=R(j,i)/r; temp=R(i,:); R(i,:)=c*R(i,:)+s*R(j,:); R(j,:)=-s*temp+c*R(j,:); temp=Q(i,:);
Q(i,:)=c*Q(i,:)+s*Q(j,:); Q(j,:)=-s*temp+c*Q(j,:); end end Q=Q'; (1) >> A=rand(6) A = 0.1509 0.4966 0.3420 0.8385 0.6946 0.1730 0.6979 0.8998 0.2897 0.5681 0.6213 0.9797 0.3784 0.8216 0.3412 0.3704 0.7948 0.2714 0.8600 0.6449 0.5341 0.7027 0.9568 0.2523 0.8537 0.8180 0.7271 0.5466 0.5226 0.8757 0.5936 0.6602 0.3093 0.4449 0.8801 0.7373 >> [Q,R]=givensqr(A) Q = 0.0959 0.5321 0.5057 0.6559 -0.0866 0.1194 0.4437 0.2382 -0.6160 0.1561 -0.5787 -0.0868 0.2406 0.6604 0.0011 -0.4495 0.3920 -0.3877 0.5468 -0.4446 0.0642 0.3670 0.3416 -0.4978 0.5428 -0.1583 0.5366 -0.4534 -0.3748 0.2153 0.3774 0.0355 -0.2697 0.0565 0.4966 0.7306 R = 1.5728 1.6903 1.0469 1.2704 1.6726 1.4082 0.0000 0.6284 0.1347 0.4430 0.5656 0.2801 0.0000 -0.0000 0.3359 0.2929 0.0738 -0.2285 0.0000 0.0000 0 0.5074 0.3593 -0.1183 0.0000 0.0000 0 0.0000 0.4600 -0.3515 0.0000 -0.0000 0 -0.0000 0 0.4319 >> Q*R-A ans = 1.0e-015 * -0.0555 0 0.0555 0.4441 0.1110 -0.1943 -0.1110 -0.2220 -0.1665 -0.1110 -0.6661 0.1110 -0.1665 -0.3331 -0.1110 -0.3331 -0.4441 -0.3331 -0.1110 -0.1110 0 -0.2220 -0.3331 -0.1110

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-0.1110 0.1110 0.1110 0.1110 0 -0.1110 -0.1110 0 0 0 -0.1110 0 >> Q'*Q ans = 1.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 1.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 1.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 1.0000 0 -0.0000 -0.0000 0.0000 0.0000 0 1.0000 We can see that Q*R is in fact equal to A. We know that Q is orthogonal
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 25

Project 4 Solutions - Project 4 Group name Unknowns Group...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online