Engrd 241: Engineering Computation 2001 Prelim 1, p.1 SOLUTIONS 1. Spreadsheet (15 points) Fill in the cells of the EXCEL spreadsheet as described below. A user has entered the values in the shaded cells. The cells already have names: C1 is named xo = starting value for x C2 is named dx = increment for x C3 is named T = constant Make entries in the following cells to accomplish the stated task. The format of the statements in Row 7 should be such that those cells can be copied to lower rows without modification. a) Cells B5, B6, B7, … should contain the value of x beginning with xo and incrementing by dx for each consecutive cell in the column. b) Cells C5, C6, C7, … should contain a function call to a Visual Basic function furb(x,T) where x is taken from the same row in the previous column. The function should define a value for a constant cc = 0.014393 and the function: furb(x)=(x1)-5{exp[cc/(x*T)]-1}. Write the Visual Basic function definition in the space below the spreadsheet (or on the back of the page.) c) Cells D6, D7, …Dj, ... should compute and store in Dj, the first order backward divided difference estimate of the derivative of furb(x) using the intervals (xo, Bj). d) Cells A5, A6, A7, … contain the step number starting with 1 and incrementing by 1 for each consecutive cell in the column. Write a conditional statement for these cells to match the following IF statement: IF (previous cell in column A is blank) Leave the cell blank ELSE IF f '(x) < 0 Leave the cell blank ELSE increment value by 1 ENDIF A B C 1 xo = 0.1 2 dx = 0.01 3 T= 270 D 4 step x f(x) 5 1 =xo =furb(B5,T) 6 =IF(A5="","",A5+1) =B5+dx =furb(B6,T) =(C6-$C$5)/(A5*dx) 7 IF(A6="","",IF(D7<0,"",B7+1)) =B6+dx =furb(B7,T) =(C7-$C$5)/(A6*dx) Function furb(x, T) cc = 0.014393 furb = (x - 1) ^( -5) * (exp(cc / (x * T)) - 1) End Function f '(x) Engrd 241: Engineering Computation 2001 Prelim 1, p.2 SOLUTIONS 2. Approximations & Errors (10 pts.) A hypothetical decimal computer stores 6 significant (decimal) digits plus a 2digit exponent within a limited range. The machine normalizes so that the first digit, c, is at least 1. Representable numbers can be written ± 0. c d d d d d 10± ef where 1≤c≤9; 0≤d≤9; 0≤ e≤ 1; 0<f<9 a) What is machine epsilon if the computer CHOPS? b) What are the largest and smallest NEGATIVE numbers? c) Exactly how many numbers can be represented in this machine? (ignore zero.) d) What is the gap between 10,000 and the next smallest number representable? Solution: a) What is machine epsilon if the computer CHOPS? Machine epsilon is the smallest number such that 1 + > 1 = b1-t b = 10, t = 6 w/ chopping, = 1.0E-5 b) What are the largest and smallest NEGATIVE numbers? SMALLEST negative number = -0.999 999 * 1019 LARGEST negative number = -0.100 000 * 10-19 c) Exactly how many numbers can be represented in this machine? (ignore zero) Total number = 2*(999,999 - 100,000 + 1) * (2*19 + 1) = 70,200,000 Ignoring zero. Not that +0 and -0 in the exponent yield the same number. d) What is the gap between 10,000 and the next smallest representable number? Gap = 0.100 000 * 10+5 – 0.999 999 *10+4 = 0.01 Engrd 241: Engineering Computation 2001 Prelim 1, p.3 SOLUTIONS 3. Rootfinding (16 pts.) For each of the cases below, describe the progress of the Newton-Raphson method when solving fi(x)=0 as either linear, superlinear, quadratic, divergent, chaotic, crawling away from a singularity, or another appropriate term, and indicate WHY such a thing might happen. Case 1 Case 2 Case 3 Case 4 Iter xi f(xi) xi f(xi) xi f(xi) xi f(xi) 1 0.6736614 6.02E+03 1 1.00E+00 0.00001 -1.05E+04 0.00001 1.00E+02 2 -2.0571781 7.83E+03 0.4995 2.51E-01 0.248749 6.29E-02 3 4.9997404 2.50E+01 0.1223644 1.60E-02 0.1024524 -3.57E+01 7.4998702 6.25E+00 6.02E+03 0.0570961 4.26E-03 0.1031534 -1.38E-01 8.7499351 1.56E+00 6 -2.0715811 7.86E+03 0.0197909 1.39E-03 0.1031561 -2.07E-06 9.3749676 3.91E-01 7 1.6972923 6.91E+03 -0.0153688 1.24E-03 0.1031561 -1.04E-13 9.6874838 9.77E-02 8 -18.211425 7.55E+03 0.0248492 1.62E-03 9.8437419 2.44E-02 9 -2523.8429 7.50E+03 -0.0076968 1.06E-03 9.9218709 6.10E-03 7.50E+03 0.0611135 4.73E-03 9.9609355 1.53E-03 11 -1.712E+16 7.50E+03 0.0223753 1.50E-03 9.9804677 3.82E-04 12 -2.198E+33 7.50E+03 -0.0111585 1.12E-03 9.9902339 9.54E-05 13 -3.625E+67 7.50E+03 0.0392297 2.54E-03 9.9951169 2.38E-05 14 -9.85E+135 7.50E+03 0.0068694 1.05E-03 9.9975585 5.96E-06 15 -7.28E+272 7.50E+03 -0.0693514 5.81E-03 9.9987792 1.49E-06 5 6.02E+03 -2.0572077 7.83E+03 0.675437 10 -47778421 0.09174 -0.0005192 1.00E+02 -6.15E+02 4 0.673665 0.0555637 -3.21E+03 Solution: CASE 1: Divergent – f '(x) close to zero; perhaps poor choice of initial value. This is the bond problem! CASE 2: Chaotic - trying to find a root where one almost exists; CASE 3: Quadratic – happens when f '(x) 0 CASE 4: Linear – Possibly due to multiple roots at x 10. Engrd 241: Engineering Computation 2001 Prelim 1, p.4 SOLUTIONS 4. One-Point Iteration (points 14) A contaminant in a solution when heated decays so that the concentration at time t > 0 is C(t) = C0 [ 0.25 exp(-2t ) + 0.75 exp(-3t) ] An engineer wants to compute the time at which the contaminant concentration is only 0.1% of the original value. [C(t) = 0.001 C0.] For large t, 0.75 exp(-3t) will be much smaller than 0.25 exp(-2t). a) Set up a simple one-point iteration scheme that can be used to compute the time t when the concentration C(t) is only 0.1% of the original value. [Suggest a scheme that should work.] Indicate why your scheme should work well. b) Illustrate the use of your scheme by performing ONE reasonable iteration. c) At what rate (linear, quadratic, superlinear, divergent ...) should your scheme converge? Is that likely to be fast enough? Solution a) 0.001 C0 = C0 [ 0.25 exp(-2t ) + 0.75 exp(-3t) ] ==> 0.001 – 0.75 e-3t = 0.25 e-2t e-2t = 0.004 – 3 e-3t ti+1 = (-1/2) ln[0.004 – 3 exp(-3ti)] b) starting with t0 = 3: t1 = -0.5* ln[0.004 – 3 exp(-3*3)] = - 0.5 * ln [ 0.004 – 0.00037 ] = - 0.5 * - 5.62 = 2.81 c) One-point iteration should converge linearly. Convergence can occur in just a few steps if 0.25 e-2t >> 0.75 e-3t as is the case. Yes, it goes fast enough. Engrd 241: Engineering Computation 2001 Prelim 1, p.5 SOLUTIONS 5. Numerical Differentiation & Truncation error (10 points) Use the central divided difference formula for f "(x) to a) Estimate the second derivative of a function f = e2x at x = 4. You choose h. b) What is the order of the truncation error? If h were decreased by a factor of 5, how much should the truncation error decrease? (See C&C page 91-95; or lecture notes on Approximation p. 6) c) As a function of epsilon , how large might the roundoff error be? Solution: f (x) = e2x f '(x) = 2 e2x f "(x) = 4 e2x a) Centered divided difference approx: f "(x) = [ f(x-h) – 2f(x) + f(x+h) ] / h2 + O(h2) With x = 4 and h = 0.1: = [ e2(3.9) – 2 e2(4) + e2(4.1) ] / (0.1)2 = [ 2.440.6 – 5,961.9 + 3,640.9 ] / 0.01 = 11, 965.2 b) The truncation error is of order O(h2). That is, if h is decreased by a factor of five, the truncation error should decrease by a factor of 25. c) The absolute value of the roundoff error should not exceed 4 |f (x)| / h2 Engrd 241: Engineering Computation 2001 Prelim 1, p.6 SOLUTIONS 6. Systems of Equations (10 points) On your first job, you are presented with a design problem that requires solving a large set of simultaneous equations (n = 1,500). The solution will require using many different right-handside vectors, but the vectors are not known at this time. They will be chosen as part of the design process. Most of the elements of the system matrix are non-zero and the matrix is not diagonally dominant. Your partner (a math major, not an engineer!) suggests computing the inverse matrix and using that to solve for each solution vector. S/he argues that, even if the inverse computation is inefficient, it only needs to be done once and that finding each new solution will be fast. a) Explain why this is not the best approach, b) Suggest an alternative and explain why it is a better choice. c) Estimate as a function of the number of equations to be solved the computational effort that would be required by the two methods. Solution: The matrix inverse is inefficient (requiring n3 flops if it is done efficiently), and it is very susceptible to roundoff error. Thus, we have the 1st law of matrix inversion: Don't do it. Gauss-Elimination is fine for one right-hand side, but begins to be inefficient for multiple solutions if they are not all available at the same time. LU decomposition would be a reasonable choice since this requires only n3/3 flops for the first solution and corresponds to repeated Gaussian elimination. Both the matrix inverse and LU require n2 flops for each additional right-hand side, so there is no gain or loss. However, the LU computations are organized to be numerically stable. Cholesky decomposition would be the specific choice if the matrix is Symetric Iterative solutions should also be considered (e.g., Gauss-Seidel). The method could be more rapid if coefficient matrix is sparse, which this is not. For large matrices, there could also be some efficiency in memory. If this matrix will be used for many right-hand sides, solutions could be optimized using relaxation. Engrd 241: Engineering Computation 2001 Prelim 1, p.7 SOLUTIONS 7. Norms and condition numbers (10 points) Compute the condition numbers of the matrix [A] using the spectral norm and the condition number of matrix [B] using the infinity norms. Are these matrices well-conditioned? 2 0 0 A = 0 5 0 0 0 4 and A = [2 0 0; 0 5 0; 0 0 4] 8 2 B= 3 1 B = [8 2; 3 1] || A ||2 = 5; 0 0.5 0 A = 0 0.2 0 0 0 0.25 = || A || || A-1 || = 2.5 ==> Matrix A is very well-conditioned. || B || = 10; 1/ 2 1 B-1 = 3 / 2 4 = || A || || A-1 || = 55 ==> Matrix A is ill-conditioned. -1 > 20 ill-conditioned < 10 well-conditioned || A-1 ||2 = 0.5 || B-1 || = 5.5 Engrd 241: Engineering Computation 2001 Prelim 1, p.8 SOLUTIONS 8 MATLAB (15 points) Write a Matlab function named funk which does the following: Begins with a comment that has your name and the date. Accepts matrix A and row vector C as part of its function call Determines the dimensions of A and C Checks to ensure that A is square, that C is a row vector, and that the number of rows in C match the number of rows in A. If any of these conditions are not met, print an error message and end execution using the STOP command. Transposes the row vector and computes the solution vector (i.e., solves the system, Ax = C. Prints the solution vector. Extracts the first row of A and stores in a new row vector R Use the first row of A to find the residual for the first equation. Prints the residual for the first equation. Nothing but the solution vector and residual (or the error messages) should be printed, with some clear text to tell the user what is being printed. function funk(A,C) [na, ma] = size(A); [nc, mc] = size(C); err = 0; if na ~= ma disp(' The matrix must be square!') stop; end if nc ~= 1 disp(' For "funk(A,C), C must be a row vector') stop; end if mc ~= na disp(' The matrix and vector dimensions do not match') stop; end The_Solution_is_x = A\C' R = A(1, :); Residual_For_Eqn_1 = C(1) - R*x end