Technische Universität München WT / Fakultät für Mathematik Prof. Dr. M. Mehl B. Gatzhammer February 7, Numerical Programming I (for CSE) Repetition ) Floating Point Numbers and Rounding a) Let f : R R be a mapping defined by f(x) 5e x 5. Compute the relative rounding error rd(f(x)) f(x) f(x), where rd(f(x)) takes the rounding errors into account when evaluating f(x). Negation does not produce any rounding errors. Is the computer-based evaluation of f stable? b) Consider the following two Matlab programs: function x function A(p,q) x -p/ + sqrt(p^-4*q)/; end function x function B(p,q) z -p/ - sqrt(p^-4*q)/; x q/z; end Both programs return a solution of the quadratic equation x + px + q. Evaluating the formulas analytically, you would get the same result x in both cases. Evaluating the formulas with Matlab, you get: input output output p q x x*x+p*x+q x x*x+p*x+q.99955978.99955978.99999995.99999995 -.8.999999.99999899999 -.57 4.99999999.999999999.9777 5..9.86576 6..76.7644947 7..99655 -.48484564 8..74558 -.54949476 9. -.. -.. -.. -. In the second column of both outputs, you can see whether the solution x fulfills x +px+q. Assign the outputs and to the programs function A and function B (give reasons for your decision!) and state which program you would choose for solving the equation x + px for some p.
Numerical Programming I (for CSE) Repetition, page a) rd(f(x)) (5(e x ( + ε ))( + ε ) 5)( + ε ). (5e x ( + ε + ε ) 5)( + ε ) (5e x 5)( + ε ) + 5e x (ε + ε )( + ε ). (5e x 5)( + ε ) + 5e x (ε + ε ) f(x)( + ε ) + 5e x (ε + ε ) rd(f(x)) f(x) f(x). [f(x)( + ε ) + 5e x (ε + ε )] f(x) f(x) ( ε + e x e x (ε + ε ) e x ) (ε + ε + ε ) ε e x ε M + e x e x ε M ε M ( + e x ) e x b) with ε i ε M (ε M machine accuracy) not stable! program output A B choose function B lim x (e x ) reason: For p q, we have p/ rd(f(x)) f(x) f(x) x p 4q so that we get cancellation in function A.
Numerical Programming I (for CSE) Repetition, page ) Interpolation Consider the mapping f : R R +, f(x) x6. a) Let p(x) be the polynomial interpolant of f with the support points P (x, y ), P (x, y ), and P (x, y ) and support abscissas x, x, x. Use the scheme of Aitken-Neville to compute p( ). b) Find the coefficients a i (i,,..., 5) of the polynomial interpolant q(x) a + a x + a x + a x + a 4 x 4 that interpolates the mapping f at the support points P i (x i, y i ) (i,,..., 4) with support abscissas x i {,,,, }. Hint: Think first! With a special ansatz, you can reduce the effort for computing q(x) significantly. a) in a triangular scheme (for x ): long version: k : x i i\k y / 9/4 p( ) y 6/ y 64 p, p, + x x (p, p, ) + / x x ( ) p, p, + x x (p, p, ) + / x x (64 ) 6.5 k : p, p, + x x x x (p, p, ) + / ( 6 ) 9 4 7.5 b) We have f(x) f(x) and f(), so we can use the ansatz q(x) a x + a 4 x 4. x : x : a + a 4 f() a + 4 a 4 6 f()
Numerical Programming I (for CSE) Repetition, page 4 solve the linear system ( ) ( ) a 4 a 4 ( ) ( ) a a 4 ) ( a a 4 ( ) 6 ( ) 5 ( ) 4 5 ) Numerical Quadrature Consider the integral where f is a function f : R R. a) Formulate a Matlab program b a f(x)dx, function y simpson sum(f,a,b,n) that computes an approximation of the value of the integral based on the Simpson sum with n subintervals, i.e. with n + function evaluations of f. b) What does the Simpson sum with n 4 subintervals return for f(x) x, a, and b 4? c) Approximating the integral with the trapezoidal sum leads to I : 4 I T 4 in case of n subintervals, I T in case of n 4 subintervals. x dx Compute an extrapolation step to get a better approximation! d) How many partitions (subintervals) n are at least necessary to compute the integral cos(x)dx with the trapezoidal sum with an error of not greater than 75? You may use the estimates sin(x), cos(x). a) function y int simpson(f,a,b,n) h (b-a)/n; xi a:h:b; y feval(f,a) + feval(f,b)... + 4*sum( feval(f,xi(::n)) )... + *sum( feval(f,xi(::n-)) ); y h/ * y; end
Numerical Programming I (for CSE) Repetition, page 5 b) Simpson sum is exact for polynomials of order 4 x dx [ ] 4 x or, alternatively: 4 x dx 4 4 (f() + 4 f() + f() + 4 f() + f(4)) ( + 4 + 8 + 6 + ) 64 c) h, h I(f) T (h ) + T (h ) T (h ) 4 (h + /h ) ( / + ) 64 d) R T S h H f () (ξ) (b a) n (b a) 4 cos(ξ) n! n 75 75 n 5 f () (ξ) 4) Solving Systems of Linear Equations Consider the linear system of equations A x b, A, b (,, ) T. a) Solve the linear system directly, using forward and backward substitution! b) Does the Jacobi method converge when applying it to solve the linear system A x b? a) LU factorization: L L L, A, U A 4 Foward-/backward substitution:...
Numerical Programming I (for CSE) Repetition, page 6 b) iteration matrix C with M D C M (A M) D (D A) eigenvalues of C: λ det(c λi) λ λ ( λ λ ) λ + λ λ( λ )! λ λ, λ, λ ρ(c) < Jacobi method converges since spectral radius of iteration matrix C is <
Numerical Programming I (for CSE) Repetition, page 7 5) Symmetric Eigenvalue Problem Consider the following Matlab code: function eigv qr iteration(a, rtol) nsize(a,); while( code ) 4 A A(:n,:n); 5 while( code ) 6 [Q,R] qr(a(:n, :n)); 7 A code 8 end 9 eigv(n) code 4a n n-; end eigv() code 4b end The function qr iteration returns a vector eigv of length n N that contains the eigenvalues of the n n input matrix A. The input parameter rtol is a positive real value and denotes the relative tolerance ε rtol of the stop criterion a n,n ε rtol a n,n that is used in the QR iteration. What has to stand in the five boxes of the given code so that the Matlab function qr iteration works properly? code : n> code : abs(a(n,n-)) > rtol*abs(a(n,n)) code : R*Q; code 4a : A(n,n); code 4b : A(,);