Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 32 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
32
Dung lượng
371,98 KB
Nội dung
Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 241 pretty(ans) (n + 1) r1 - r-1 r-1 (c) symsum(xˆk/factorial(k), k, 0, Inf) Error using ==> factorial N must be a matrix of non-negative integers. Here are two ways around this difficulty. symsum(xˆk/gamma(k + 1), k, 0, Inf) ans = exp(x) symsum(xˆk/sym(’k!’), k, 0, Inf) ans = exp(x) (d) symsum(1/(z - k)ˆ2, k, -Inf, Inf) ans = piˆ2+piˆ2*cot(pi*z)ˆ2 242 Solutions to the Practice Sets 7. (a) taylor(exp(x), 7, 0) ans = 1+x+1/2*xˆ2+1/6*xˆ3+1/24*xˆ4+1/120*xˆ5+1/720*xˆ6 (b) taylor(sin(x), 5, 0) ans = x-1/6*xˆ3 taylor(sin(x), 6, 0) ans = x-1/6*xˆ3+1/120*xˆ5 (c) pretty(taylor(sin(x), 6, 2)) 23 sin(2) + cos(2) (x - 2) - 1/2 sin(2) (x - 2) - 1/6 cos(2) (x - 2) 45 + 1/24 sin(2) (x - 2) + 1/120 cos(2) (x - 2) (d) taylor(tan(x), 7, 0) ans = x+1/3*xˆ3+2/15*xˆ5 Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 243 (e) taylor(log(x), 5, 1) ans = x-1-1/2*(x-1)ˆ2+1/3*(x-1)ˆ3-1/4*(x-1)ˆ4 (f) pretty(taylor(erf(x), 9, 0)) 35 7 xx x x 2 - 2/3 + 1/5 - 1/21 1/2 1/2 1/2 1/2 pi pi pi pi 8. (a) syms x y; ezsurf(sin(x)*sin(y), [-3*pi 3*pi -3*pi 3*pi]) −5 0 5 −5 0 5 −1 −0.5 0 0.5 1 x sin(x) sin(y) y (b) syms x y; ezsurf((xˆ2 + yˆ2)*cos(xˆ2 + yˆ2), [-1 1 -1 1]) 244 Solutions to the Practice Sets −1 −0.5 0 0.5 1 −1 0 1 −1 −0.5 0 0.5 1 x (x 2 +y 2 ) cos(x 2 +y 2 ) y 9. T = 0:0.01:1; for j = 0:16 fill(4*cos(j*pi/8)+(1/2)*cos(2*pi*T), 4*sin(j*pi/8)+(1/2)*sin(2*pi*T), ’r’); axis equal; axis([-5 5 -5 5]); M(j+1) = getframe; end movie(M) −5 0 5 −5 0 5 Obviously we can’t show the movie in this book, but you see the last frame above. Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 245 10. (a) A1=[345;2-37;1-61];b=[2;-1;3]; format short; x = A1\b x= 2.6196 -0.2283 -0.9891 A1*x ans = 2.0000 -1.0000 3.0000 (b) A2 = [3 -9 8; 2 -3 7; 1 -6 1]; b = [2; -1; 3]; x = A2\b Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.189521e-18. x= -6.0000 -1.3333 1.0000 A2*x ans = 2 -1 3 The matrix A2 is singular. In fact, det(A2) ans = 0 246 Solutions to the Practice Sets (c) A3 = [1 3 -2 4; -2 3 4 -1; -4 -3 1 2; 2 3 -4 1]; b3 = [1; 1; 1; 1]; x = A3\b3 x= -0.5714 0.3333 -0.2857 0 A3*x ans = 1.0000 1.0000 1.0000 1.0000 (d) syms a b c d x y u v; A4 = [a b; c d]; A4\[u; v] ans = (d*u-b*v)/(d*a-b*c) -(c*u-v*a)/(d*a-b*c) det(A4) ans = d*a-b*c The determinant of the coefficient matrix is the denominator in the answer. So one gets an answer only if the coefficient matrix is non-singular. Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 247 11. (a) rank(A1) ans = 3 rank(A2) ans = 2 rank(A3) ans = 4 rank(A4) ans = 2 MATLAB implicitly assumes that ad − bc is not 0 here. (b) Only the second one computed is singular. 248 Solutions to the Practice Sets (c) det(A1) ans = 92 inv(A1) ans = 0.4239 -0.3696 0.4674 0.0543 -0.0217 -0.1196 -0.0978 0.2391 -0.1848 det(A2) ans = 0 The matrix A2 does not have an inverse. det(A3) ans = 294 inv(A3) ans = 0.1837 -0.1531 -0.2857 -0.3163 0 0.1667 0 0.1667 0.1633 0.0306 -0.1429 -0.3367 0.2857 -0.0714 0 -0.2143 det(A4) Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 249 ans = d*a-b*c inv(A4) ans = [ d/(d*a-b*c), -b/(d*a-b*c)] [ -c/(d*a-b*c), a/(d*a-b*c)] 12. (a) [U1, R1] = eig(A1) U1 = -0.9749 0.6036 0.6036 -0.2003 0.0624 + 0.5401i 0.0624 - 0.5401i 0.0977 -0.5522 + 0.1877i -0.5522 - 0.1877i R1 = 3.3206 0 0 0 -1.1603 + 5.1342i 0 0 0 -1.1603 - 5.1342i A1*U1 - U1*R1 ans = 1.0e-14 * 0.3109 0.2776 - 0.3997i 0.2776 + 0.3997i -0.0444 0 - 0.0777i 0 + 0.0777i -0.0833 0.0999 - 0.1776i 0.0999 + 0.1776i This is essentially zero. Notice the e-14. [U2, R2] = eig(A2) 250 Solutions to the Practice Sets U2 = 0.9669 0.7405 0.7405 0.1240 0.4574 - 0.2848i 0.4574 + 0.2848i -0.2231 0.2831 + 0.2848i 0.2831 - 0.2848i R2 = -0.0000 0 0 0 0.5000 + 6.5383i 0 0 0 0.5000 - 6.5383i A2*U2 - U2*R2 ans = 1.0e-14 * -0.2224 -0.2554 - 0.2665i -0.2554 + 0.2665i -0.1498 0.0888 0.0888 -0.1156 -0.0222 + 0.0666i -0.0222 - 0.0666i Same comment as in (a). [U3, R3] = eig(A3); max(abs(A3*U3 - U3*R3)) ans = 1.0e-14 * 0.4529 0.4529 0.5245 0.5245 Ditto yet again. (Note that we suppressed the output of the first command, and took the maximum of the absolute values of the entries in each column of A3*U3 - U3*R3, in order to keep the data from scrolling off the screen.) [U4, R4] = eig(A4); pretty(U4) [ 2 2 1/2 [ 1/2d-1/2a-1/2(d -2da+a +4bc) [- , [c [...]... and make it easier to change this value later Then we set up a zero matrix A of the appropriate sizes and begin a loop that successively defines each row of the matrix Finally, we extract the maximum value from the list of eigenvalues of A n = 500; A = zeros(n); for k = 1:n A( k,:) = 1./(k:(k + n - 1)); end max(eig (A) ) ans = 2.37 69 5 Again we display below our solution and its output First we define a. .. avg: 0.38 Worst average: 0. 294 Lifetime avg: 0.3 39 Solutions to Practice Set C: Developing Your MATLAB Skills 265 (d) The average for the five different 20-year careers is: (ave1 + ave2 + ave3 + ave4 + ave5)/5 ans = 0.3366 Not bad In fact if we ran the simulation 100 times and took the average it would be very close to 0.338 4 Our solution and its output is below First we set n to 500 in order to save... bats in the year, although we shall use only 500 yearbattingaverage = @(n) sum(rand(n, 1) < 0.338)/n; Now we run the function format short yearbattingaverage(500) ans = 0.3380 (b) Now let’s write a function M-file that simulates a 20-year career As with the number of at bats in a year, we’ll allow for a varying-length career type career 264 Solutions to the Practice Sets function ave = career(n, k) %... the batting average for each % year in a k-year career, assuming n at bats in each year % Then it lists the average for each of the years in the % career, and finally computes a lifetime average Y = zeros(1, k); for j = 1:k Y(j) = sum(rand(n, 1) < 0.338)/n; end ave = sum(Y)/k; disp([’Best avg: ’, num2str(max(Y), 4)]) disp([’Worst average: ’, num2str(min(Y), 4)]) disp([’Lifetime avg: ’, num2str(ave,... vector t of values between 0 and 2π, in order to be able later to represent circles parametrically as x = r cos t, y = r sin t Then we clear any previous figure that might exist and prepare to create the figure in several steps Let’s say that the red circle will have radius 1; then the first black ring should have inner radius 2 and outer radius 3, and thus the tenth black ring should have inner radius... Incidentally, the bank model yields in this case 10000*(((1 + 0.08)ˆ6 - 1)/0.08 - 1) ans = 633 59. 29 which is better than some investment models and worse than others 3 (a) We can use the expression (rand < 0.338) to compute whether Tony gets a hit or not during a single at bat, based on a random number chosen between 0 and 1 If the random number is less than 0.338, the expression evaluates to 1 and Tony... the simulation ave1 = career(500, 20); Best avg: 0. 39 Worst average: 0. 292 Lifetime avg: 0.3 394 (c) Now we run the simulation four more times: ave2 = career(500, 20); Best avg: 0.372 Worst average: 0. 294 Lifetime avg: 0.334 ave3 = career(500, 20); Best avg: 0.382 Worst average: 0.306 Lifetime avg: 0.3 39 ave4 = career(500, 20); Best avg: 0.36 Worst average: 0. 292 Lifetime avg: 0.3314 ave5 = career(500,... colormap(’gray’) 50 40 30 20 10 0 0 10 20 30 40 50 It is not so clear from the picture where to hide, although it looks as if the Captain has a pretty good chance of surviving a small number of shots But 100 shots may be enough to find him Intuition says he ought to stay close to the boundary (b) Below is a series of commands that places Picard at the center of the arena, fires the death ray 100 times, and. .. a hit; otherwise, the expression evaluates to 0 and Tony is retired by the opposition We could simulate a year in Tony’s career by evaluating this expression 500 times in a loop More simply, we can put 500 random numbers into this expression at once and sum the results, dividing by 500 to get his batting average for the year The following function does just this, allowing more generally for n at bats... 20 and outer radius 21 We start drawing from the outside in because the idea is to fill the largest circle in black, then fill the next largest circle in white leaving only a ring of black, then fill the next largest circle in black leaving a ring of white, etc The if statement tests true when r is odd and false when it is even We stop the alternation of black and white at a radius of 2 in order to make . one gets an answer only if the coefficient matrix is non-singular. Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 247 11. (a) rank (A1 ) ans = 3 rank (A2 ) ans = 2 rank (A3 ) ans = 4 rank (A4 ) ans. but a coward dies a thousand deaths. But the person who said that probably never encountered a Cardassian. Long live Picard! Solutions to Practice Set C: Developing Your MATLAB Skills 2 59 2. (a) Consider. -0.14 29 -0.3367 0.2857 -0.0714 0 -0.2143 det (A4 ) Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra 2 49 ans = d *a- b*c inv (A4 ) ans = [ d/(d *a- b*c), -b/(d *a- b*c)] [ -c/(d *a- b*c), a/ (d *a- b*c)] 12. (a) [U1,