Ma 449: Numerical Applied Mathematics Model Solutions to Homework Assignment 3 Prof. Wickerhauser Exercise 1(iii) of Section 3.1, p.108. < M A T L A B > Version of 25 May 1982 Ported to NeXT 30 Nov 1991 HELP is available <> x=[4,-8,1]; y=[1,-12,-11]; <> x+y ANS = 5. -20. -10. <> x-y ANS = 3. 4. 12. <> 3*x ANS = 12. -24. 3. <> norm(x) ANS = 9. <> 7*y-4*x ANS = -9. -52. -81. <> x*y' ANS = 89. <> norm(7*y-4*x) ANS = 96.6747 Exercise 4(c) of Section 3.1, p.108. <>a=[-1 9 4;2 -3 -6; 0 5 7] A = -1. 9. 4. 2. -3. -6. 0. 5. 7. <>b=[-4 9 2; 3 -5 7; 8 1 -6] B = -4. 9. 2. 3. -5. 7. 8. 1. -6. <>3*a-2*b ANS = 5. 9. 8. 0. 1. -32. -16. 13. 33. Exercise 5(a) of Section 3.1, p.108. <>A = [-2 5 12; 1 4 -1; 7 0 6; 11 -3 8] = -2. 5. 12. 1. 4. -1. 7. 0. 6. 11. -3. 8. <>A' = -2. 1. 7. 11. 5. 4. 0. -3. 12. -1. 6. 8. Exercise 2 of Section 3.2, p.118. <>A=[1 -2 3; 2 0 5]; B=[3 0;-1 5; 3 -2]; <>A*B = 14. -16. 21. -10. <>B*A = 3. -6. 9. 9. 2. 22. -1. -6. -1. Exercise 5(a*) of Section 3.2, p.118. Use Matlab: <> a=[-1 -7; 5 2]; det(a) ANS = 33. or else compute det(a)= (-1)(2)-(5)(-7)= -2+35 = 33. Exercise 5(b) of Section 3.2, p.118. <>A=[2 0 6; -1 5 -4; 3 -5 2]; det(A) = -80. Algorithm 1 of Section 3.2, p.119. Use help plot3 to get some documentation on plotting. The following command draws a polygon through the points (x(1),y(1),z(1)) ,..., (x(n),y(n),z(n)), where x,y,z are column vector of length n: <> plot3(x,y,z) To get a cube, imagine tracing the edges in sequence and then put the vertex coordinates into x,y, and z in the order they are encountered. It is OK to retrace edges; the following solution with n=16 that retraces 3 edges: Vertices in the order they are encountered: <> u = [ 0 0 0; 1 0 0; 1 0 1; 1 1 1; 1 1 0; 0 1 0; 0 1 1; 0 0 1; 0 0 0; 0 1 0; 0 1 1; 1 1 1; 1 1 0; 1 0 0; 1 0 1; 0 0 1; 0 0 0] Copy columns into x,y,z and plot: <> x=u(:,1); y=u(:,2); z=u(:,3); plot3(x,y,z) Rotate all points, extract columns, and replot: <>pi=4*atan(1) = 3.1416 <>ay=pi/6 = .5236 <>Ry=[cos(ay) 0 sin(ay); 0 1 0; -sin(ay) 0 cos(ay)] = .8660 .0000 .5000 .0000 1.0000 .0000 -.5000 .0000 .8660 <>az=pi/4 = .7854 <>Rz=[cos(az) -sin(az) 0; sin(az) cos(az) 0; 0 0 1] = .7071 -.7071 .0000 .7071 .7071 .0000 .0000 .0000 1.0000 <> v=(Rz*Ry*u')'; x=v(:,1); y=v(:,2); z=v(:,3); plot3(x,y,z) Exercise 3 of Section 3.3, p.124. Use back substitution in Matlab: <> x5=6/3 X5 = 2. <> x4=(10+x5)/(-2) X4 = -6. <> x3=3+x4+2*x5 X3 = 1. <> x2=(0-6*x3-2*x4-7*x5)/(-2) X2 = 4. <> x1=(4+x2-2*x3-2*x4+x5)/4 X1 = 5. Exercise 6 of Section 3.3, p.124. <>A=[5 0 0 0;1 3 0 0;3 4 2 0;-1 3 -6 -1] = 5. 0. 0. 0. 1. 3. 0. 0. 3. 4. 2. 0. -1. 3. -6. -1. <>b=[-10 4 2 5]' = -10. 4. 2. 5. <>det(A) = -30.0000 <>inv(A)*b = -2.0000 2.0000 .0000 3.0000 Algorithm 2 of Section 3.3, p.125. MATLAB PROGRAM: Write the following into the file "forsub.m": % Expect lower triangular matrix A and vector b: function x = forsub(A, b) N = length(b); % expect NxN matrix A, Nx1 vector b for i=1:N x(i) = b(i); % start with i'th right-hand side for j=1:(i-1) x(i) = x(i) -A(i,j)*x(j); % subtract prior x values end x(i) = x(i)/A(i,i); % divide by x(i)'s row-i coefficient end INPUT: Test this program with the matrix of Exercise 3 in this section, reverse-indexed so that it is lower rather than upper triangular:: >> a=[3 0 0 0 0; -1 -2 0 0 0; -2 -1 1 0 0; 7 2 6 -2 0; -1 2 2 -1 4] a = 3 0 0 0 0 -1 -2 0 0 0 -2 -1 1 0 0 7 2 6 -2 0 -1 2 2 -1 4 >> b=[6 10 3 0 4]' b = 6 10 3 0 4 OUTPUT: Check agreement with (x1,x2,x3,x4,x5) from Exercise 3: >> forsub(a,b) ans = 2 -6 1 4 5 Algorithm 3 of Section 3.3, p.125. Apply forsub() to a matrix constructed on the command line: >> for i=1:20 b(i,1)=i; for j=1:i a(i,j)=i+j; end end >> b b = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >> a a = 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 7 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 8 9 10 11 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 9 10 11 12 13 14 0 0 0 0 0 0 0 0 0 0 0 0 0 9 10 11 12 13 14 15 16 0 0 0 0 0 0 0 0 0 0 0 0 10 11 12 13 14 15 16 17 18 0 0 0 0 0 0 0 0 0 0 0 11 12 13 14 15 16 17 18 19 20 0 0 0 0 0 0 0 0 0 0 12 13 14 15 16 17 18 19 20 21 22 0 0 0 0 0 0 0 0 0 13 14 15 16 17 18 19 20 21 22 23 24 0 0 0 0 0 0 0 0 14 15 16 17 18 19 20 21 22 23 24 25 26 0 0 0 0 0 0 0 15 16 17 18 19 20 21 22 23 24 25 26 27 28 0 0 0 0 0 0 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 0 0 0 0 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 0 0 0 0 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 0 0 0 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 0 0 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 0 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 >> forsub(a,b) ans = Columns 1 through 15 0.5000 0.1250 0.0625 0.0391 0.0273 0.0205 0.0161 0.0131 0.0109 0.0093 0.0080 0.0070 0.0062 0.0055 0.0050 Columns 16 through 20 0.0045 0.0041 0.0038 0.0035 0.0032