Oldexam Ws0708 Solution

  • Upload
    hisuin

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

  • 7/28/2019 Oldexam Ws0708 Solution

    1/14

  • 7/28/2019 Oldexam Ws0708 Solution

    2/14

    Numerical Programming I (for CSE) Final Exam, page 2

    1) Floating Point Numbers and Rounding ( 4+2 = 6 points)

    a) Let f : R R be a mapping defined by

    f(x) = 5ex 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 doesnot 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/2 + sqrt(p^2-4*q)/2;

    end

    function x = function B(p,q)

    z = -p/2 - sqrt(p^2-4*q)/2;x = q/z;

    end

    Both programs return a solution of the quadratic equation x2 + px + q = 0. Evaluating theformulas analytically, you would get the same result x in both cases. Evaluating the formulaswith Matlab, you get:

    input output 1 output 2

    p q x x*x+p*x+q x x*x+p*x+q

    101 1 0.09901951359278 0 0.09901951359278 0102 1 0.00999900019995 0 0.00999900019995 -0.00000000000028103 1 0.00099999900000 0 0.00099999899999 -0.00000000001507104 1 0.00009999999900 0 0.00009999999929 0.00000000292777105 1 0.00001000000000 0 0.00001000000339 0.00000033863576106 1 0.00000100000000 0 0.00000100000761 0.00000761449437107 1 0.00000010000000 0 0.00000009965152 -0.00348484516143108 1 0.00000001000000 0 0.00000000745058 -0.25494194030762109 1 0.00000000100000 0 0 -1.00000000000000

    1010 1 0.00000000010000 0 0 -1.000000000000001011

    1 0.00000000001000 0 0 -1.00000000000000

    1012 1 0.00000000000100 0 0 -1.00000000000000

    In the second column of both outputs, you can see whether the solution x fulfills x2+px+q 0.

    Assign the outputs 1 and 2 to the programs function A and function B (give reasons for yourdecision!) and state which program you would choose for solving the equation x2 +px 1 = 0for some p 0.

  • 7/28/2019 Oldexam Ws0708 Solution

    3/14

    Numerical Programming I (for CSE) Final Exam, page 3

    Solution:

    a)

    rd(f(x)) = (5(ex(1 + 1))(1 + 2) 5)(1 + 3).

    = (5ex(1 + 1 + 2)

    5)(1 + 3)

    = (5ex 5)(1 + 3) + 5ex(1 + 2)(1 + 3).

    = (5ex 5)(1 + 3) + 5ex(1 + 2)= f(x)(1 + 3) + 5e

    x(1 + 2)

    rd(f(x)) f(x)f(x) .=

    [f(x)(1 + 3) + 5ex(1 + 2)] f(x)

    f(x)

    = 3 +

    ex

    ex

    1(1 + 2) =

    ex(1 + 2 + 3) 3ex

    1 M +

    ex

    ex 1 2M

    = M

    1 + 2

    ex

    ex 1

    with |i| M (M machine accuracy)

    limx0

    (ex 1) = 0

    rd(f(x)) f(x)f(x)

    x0

    not stable!b)

    program outputA 2B 1

    choose function B

    reason: For p q, we have p/2 p24q

    2so that we get cancellation in function A.

  • 7/28/2019 Oldexam Ws0708 Solution

    4/14

    Numerical Programming I (for CSE) Final Exam, page 4

    2) Interpolation ( 3+3+1+1 = 8 points)Consider the mapping

    f : [0, 2] [0, 1], f(x) = sinx

    2

    .

    a) To approximate the mapping f, find the interpolant p(x) that interpolates f at the three

    support points P0, P1, and P2 with support abscissas

    x0 = 0, x1 = 1, x2 = 2.

    Compute the divided differences and give a closed representation of the interpolant

    p(x) = a0 + a1 (x x0) + a2 (x x0)(x x1).

    with the help of Newtons interpolation formula.

    b) To get a higher accuracy, another support point P3 with support abscissa x3 =13

    is added.

    Find the new interpolant that interpolates the four points P0, P1, P2, and P3.

    c) Let p1(u) be the interpolant with general support abscissas {u0, u1, u2, u3} and p2(u) be theinterpolant with general support abscissas {u1, u2, u3, u4}. Find the interpolant p3(u) withsupport abscissas {u0, u1, u2, u3, u4} based on p1(u) and p2(u)!

    d) Which kind of interpolant would you choose, if 50 support points had to be interpolated?Give reasons for your answer!

  • 7/28/2019 Oldexam Ws0708 Solution

    5/14

    Numerical Programming I (for CSE) Final Exam, page 5

    Solution:

    a) in a triangular scheme:

    xi i\k 0 1 2 30 0 y0 = 0 1 1

    1 1 y1 = 1 1

    2 2 y2 = 0

    long version:

    [x0] f = sin(0) = 0

    [x1] f = sin(

    2) = 1

    [x2] f = sin() = 0

    [x0, x1] f =[x1]f [x0]f

    x1 x0 =1 01 0 = 1

    [x1, x2] f =[x2]f [x1]f

    x2 x1 =0 12 1 = 1

    [x0, x1, x2] f =[x1, x2]f [x0, x1]f

    x2 x0=

    1 12 0

    =

    1

    p(x) = [x0]f + [x0, x1]f (x x0) + [x0, x1, x2]f (x x0) (x x1)= 0 + 1 (x 0)) + ( 1 )(x 0))(x 1)= 0 + 1 (x 0) 1 (x 0)(x 1)= x x(x 1) = x(2 x) = 2x x2

    b) extension of the triangular scheme:

    xi i\k 0 1 2 30 0 y0 = 0 1 1

    320

    1 1 y1 = 1 1 2120 2 2 y2 = 0 - 31013

    3 y3 =12

    long version:

    [x3] f = sin(

    6) =

    1

    2

    [x2, x3] f =[x3]f [x2]f

    x3 x2 =12 013 2

    = 310

    [x1, x2, x3] f =[x2, x3]f [x1, x2]f

    x3 x1 = 310 + 113 1

    = 2120

    [x0, x1, x2, x3] f =[x1, x2, x3]f [x0, x1, x2]f

    x3 x0 =2120 + 113 0

    = 320 .

    p(x) = x(2 x) 3

    20 x(x 1)(x 2)

  • 7/28/2019 Oldexam Ws0708 Solution

    6/14

    Numerical Programming I (for CSE) Final Exam, page 6

    c)

    p3(u) = p1(u) u u4u0 u4 + p2(u)

    u u0u4 u0

    = p1(u) +u u0u4 u0 (p2(u) p1(u))

    d) spline interpolation

    polynomial interpolation would oscillate at the boundaries because of the high polynomialdegree

  • 7/28/2019 Oldexam Ws0708 Solution

    7/14

    Numerical Programming I (for CSE) Final Exam, page 7

    3) Numerical Quadrature ( 4+1+3+3 = 11 points)Consider the integral b

    a

    f(x)dx,

    where f is a function f : R R.

    a) Formulate a Matlab program

    function y = simpson sum(f,a,b,n)

    that computes an approximation of the value of the integral based on the Simpson sum withn subintervals, i.e. with n + 1 function evaluations of f.

    b) What does the Simpson sum with n = 4 subintervals return for f(x) = x2, a = 0, and b = 4?

    c) Approximating the integral

    I :=

    4

    0

    x2dx

    with the trapezoidal sum leads to

    I T1 = 24 in case of n1 = 2 subintervals,I T2 = 22 in case of n2 = 4 subintervals.

    Compute an extrapolation step to get a better approximation! How many further extrap-olation steps (e.g. using the trapezoidal sum with n3 = 8, n4 = 16,. . . subintervals) arereasonable? Why?

    d) How many partitions (subintervals) n are at least necessary to compute the integral

    10

    cos(2x)dx

    with the trapezoidal sum with an error of not greater than 175

    ?

    You may use the estimates | sin(x)| 1, | cos(x)| 1.

  • 7/28/2019 Oldexam Ws0708 Solution

    8/14

    Numerical Programming I (for CSE) Final Exam, page 8

    Solution:

    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(2:2:n)) ) ...+ 2*sum( feval(f,xi(3:2:n-1)) );

    y = h/3 * y;

    end

    b) Simpson sum is exact for polynomials of order 2

    40

    x2dx =

    1

    3x340

    = 311

    3

    or, alternatively:

    40

    x2dx 404

    3 (f(0) + 4 f(1) + 2 f(2) + 4 f(3) + f(4))

    =1

    3 (0 + 4 + 8 + 36 + 32) = 64

    3= 21

    1

    3

    c) h1 = 2, h2 = 1

    I(f) T(h2) +T(h2)

    T(h1)

    (h21/h22) 1 = 22 +22

    24

    (22/12) 1 = 22 + 2

    3 = 21

    1

    3 =

    64

    3

    one extrapolation step is enough, no more are necessary: the trapezoidal rule is exact forlinear functions, i.e. O(h2i )extrapolation leads to O(h21 h22) and, thus, it is exact for quadratic functions

    d)

    |RTS| =h2 H f

    (2)()

    12

    =(b a)2

    n2 (b a)

    f(2)()

    12

    = 1n24cos(2)12

    13n2!

    175

    n

    75

    3= 5

  • 7/28/2019 Oldexam Ws0708 Solution

    9/14

    Numerical Programming I (for CSE) Final Exam, page 9

    4) Solving Systems of Linear Equations ( 2+3+2+2 = 9 points)Consider the following Matlab code:

    function A = func(A)

    for k=1:n

    for j=1:k-1A(k,k)=A(k,k)-A(k,j)^2;

    end

    A(k,k) = sqrt(A(k,k));

    for i=k+1:n

    for j=1:k-1

    A(i,k) = A(i,k)-A(i,j)*A(k,j);

    end

    A(i,k)=A(i,k)/A(k,k);

    end

    end

    end

    a) Which algorithm is implemented by the program func(A) and what does it return as result?What is the complexity of the algorithm?

    b) Find an LU factorization of

    A =

    4 2 32 3 0

    4 4 9

    .

    c) Give two reasons for pivoting (in general)!d) Why are direct solving methods not suitable for large sparse systems of linear equations?

  • 7/28/2019 Oldexam Ws0708 Solution

    10/14

    Numerical Programming I (for CSE) Final Exam, page 10

    Solution:

    a) Cholesky factorization (in situ), complexity: O(n3) (for A Rnn)output: symmetric input matrix A where the lower triangular part is replaced by the Choleskyfactor L

    b)

    L1 =

    1 0 01

    21 0

    1 0 1

    , A1 =

    4 2 30 2 3

    2

    0 6 6

    L =

    1 0 01

    21 0

    1 3 1

    , U =

    4 2 30 2 3

    2

    0 0 32

    c) avoid zeros as diagonal elements (division by zero!)

    stability of solution (rounding errors!)

    d) fill-ins

    complexity

    often, the exact solution is not needed and an approximate solution is sufficient (LUfactorization would be too much effort!)

  • 7/28/2019 Oldexam Ws0708 Solution

    11/14

    Numerical Programming I (for CSE) Final Exam, page 11

    5) Solving Ordinary Differential Equations ( 2+3+2+4 = 11 points)Consider the initial value problem

    y(t) = (y(t))2 t 1,y(1) = 1.

    It is known that y(t) > 0 for all t 1.

    a) Solve the initial value problem analytically via separation of the variables.

    b) Let yk be a numerical approximation of y(tk). Apply the implicit Euler method (backwardEuler method) to find an approximation yk+1 y(tk+1) in tk+1 = tk + h.Perform one step of the implicit Euler method with stepwidth h = 1

    4to find an approximation

    of the solution of the initial value problem at t = 1.25 (note that y(t) > 0).

    c) Instead of solving the implicit Euler formula directly, the value yk+1 could also be computed

    iteratively by solving the non-linear equation numerically. The three plots of figure 1 show theerror reduction (depending on the number of iteration steps) for the following three methodsexemplarily when solving the non-linear equation of b):

    (A) the bisection method (with initial interval [0, 10]),

    (B) Newtons method (with initial value 10)

    (C) the secant method (with initial interval [0, 10]).

    Assign every method one of the plots!

    d) Apply Newtons method to solve the non-linear equation of b) for yk+1 numerically: For this

    purpose, specify an appropriate function g and show that the application of Newtons methodto the function g leads to the iteration rule

    y(j+1)k+1 :=

    h (y(j)k+1)2 + yk2hy(j)k+1 + 1

    ,

    where j denotes the index of Newtons iteration. What might be a good initial value y(0)k+1?

    Why is the choice of the initial point important?

  • 7/28/2019 Oldexam Ws0708 Solution

    12/14

    Numerical Programming I (for CSE) Final Exam, page 12

    plot 1 plot 2

    plot 3

    Figure 1: Solving non-linear equations: error plotted against the number of iteration steps (semi-logarithmic plots)

  • 7/28/2019 Oldexam Ws0708 Solution

    13/14

    Numerical Programming I (for CSE) Final Exam, page 13

    Solution:

    a)

    dy

    dt= y2

    1y2

    dy = 1 dt

    yy(t0)

    1

    2d =

    tt0

    d

    y1

    1

    2d =

    t1

    d

    1

    y 1 = t 1

    y(t) =1

    t

    b)

    yk+1 = yk + h f(yk+1, tk+1)= yk h y2k+1

    Remark: The explicit solution is given by

    0 = hy2k+1 + yk+1

    yk

    yk+1 =1 1 + 4hyk

    2hy(t)>0= yk+1 = 1 +

    1 + 4hyk

    2h

    Computing the first step:

    y0 = 1

    y1 = y0 h y21

    y1 = 1

    1 + 4

    14

    1

    2 14

    = 2(1 2)y1 = 2(

    2 1) since y(t) > 0

    c)

    plot method1 C (secant method)2 B (Newtons method)3 A (bisection)

  • 7/28/2019 Oldexam Ws0708 Solution

    14/14

    Numerical Programming I (for CSE) Final Exam, page 14

    d)

    g(yk+1) = hy2k+1 + yk+1 yk

    g(yk+1) = 2hyk+1 + 1

    y(j+1)k+1 = y

    (j)k+1

    g(y(j)k+1)

    g

    (y(j)k+1)

    = y(j)k+1

    h(y(j)k+1)

    2 + y(j)k+1 yk

    2hy(j)k+1 + 1

    =2h (y(j)k+1)2 + y(j)k+1 h(y(j)k+1)2 y(j)k+1 + yk

    2hy(j)k+1 + 1

    =h (y(j)k+1)2 + yk

    2hy(j)k+1 + 1

    good choice: yk(alternatively: yk + h f(yk) = yk h y2k (explicit Euler step))Newtons method is locally quadratically convergent! It is not global convergent!