FYTN HT18 Dept. of Astronomy and Theoretical Physics Lund University, Sweden Exercises, module A (ODEs, numerical integration etc) 1. Use Euler s method to solve y (x) = y(x), y() = 1. (a) Determine y after n steps of size h, y n. What is the exact solution? (b) For which values of h is the sequence y,y 1,... bounded? (c) Let y(x,h) denote the estimate of y(x) obtained for step size h. Determine the global error y(x,h) y(x) to leading order in h, for fixed x.. Solve the initial value problem y (x) = xy(x), y() = 1. What is the analytic solution? Use the nd order Runge-Kutta with a = b = 1/ and m = 1 and step size h =.. Compare the results from the Runge-Kutta method with the exact result at time t = h. What is the relative error? Compare also to the relative error obtained using the Euler method.. Use the Euler-Maclaurin summation formula to show that n m = 1 n(n+1) and n m = 1 6 n(n+1)(n+1) 4. Calculate 4 dx x with the trapezoidal rule for the step sizes.5,.5 och.15. Improve the values by Richardson extrapolation. 5. Calculate the integral in exercise 4 by using two-point Gaussian integration, after a change of variable to t = x 7. 6. Determine an integration formula of the type f(x)(1+x )dx A 1 f(x 1 )+A f(x ) that is exact for all polynomials with degree less than 4. 7. Consider the boundary value problem y (x) y(x) = with y() = and y(1) = sinh(1). What is theanalytic solution? Solve this problemusingthe matrix methodwith a step size h =.5 (this gives -equation system to be diagonalized). Compare your numerical solution to the analytic result.
Solutions 1. Use Euler s method to solve y (x) = y(x), y() = 1. (a) Determine y after n steps of size h, y n. What is the exact solution? Solution: Theanalytic solution is y(x) = e x - we look for afunction whosederivative is proportional to itself, this is clearly an exponential function. If we would rather want to derive this result, note that the equation above can be written y (x)/y(x) =, or equivalently dln[y(x)]/dx =. Integrating from to x on both sides give ln[y(x)] = x, or, y(x) = e x. Let us now turn to the Euler algorithm. Discretizing x via x n = nh with a step-size h and defining y n = y(x n ), the Euler algorithm becomes: y = 1 y 1 y(+h) = 1 h y y(+h) = (1 h) y n y(+nh) = (1 h) n (b) For which values of h is the sequence y,y 1,... bounded? Solution: A bounded sequence is one for which, for all n [, ], y n remains finite. So for our case, where y n = (1 h) n, we must require h. (c) Let y(x,h) denote the estimate of y(x) obtained for step size h. Determine the global error y(x,h) y(x) to leading order in h, for fixed x. Solution: Using the fact that n = x/h, we write y n = y(x,h) = (1 h) x/h. Note that we have y(x,h) = exp[lny(x,h)] = exp[(x/h)ln(1 h)], so that using the Taylor expansion of the logarithm ln[1+z] z z /, we have y(x,h) exp[ x(1+h/)] exp( x) = y(x,h = ) = y(x) when h as it should. We now proceed by Taylor expanding the the approximate solution, y(x, h), for small h according to y(x,h) = y(x,h = )+h dy(x,h) h= +... dh Using the fact that (use the Taylor expansion of the logarithm to second order and 1/(1+z) 1 z for small z below) ( dy(x, h) = (1 h) x/h x dh h(1 h) + xln(1 h) ) h xe x / as h and that y(x,h = ) = y(x) we find y(x,h) y(x) h xe x /+O(h ) i.e. the global error scales linearly with h as it should for the Euler algorithm. Note that we here, besides the correct scaling with h, also calculated an explicit expression for the prefactor in the global error estimate.
. Solve the initial value problem y (x) = xy(x), y() = 1. What is the analytic solution? Use the nd order Runge-Kutta with a = b = 1/ and m = 1 and step size h =.. Compare the results from the Runge-Kutta method with the exact result at time t = h. What is the relative error? Compare also to the relative error obtained using the Euler method. Solution: We write y (x) = f(x,y(x)) with f(x,y(x)) = xy(x). The analytic solution is y(x) = exp( x /). as can be derived using the integrating factor method, for instance. The nd order Runge-Kutta method is k 1 = hf(x n,y n ) k = hf(x n +mh,y n +mk 1 ) y n+1 = y n +ak 1 +bk where y n = y(x n ) and x n = nh. The first iteration gives: k 1 = hx y =. 1 = k = h(x +mh)(y +mk 1 ) = (.) =.4 y 1 = y +ak 1 +bk =.98 with a = b = 1/ and m = 1. The second iteration is: k 1 = hx 1 y 1 =...98 =.9 k = h(x 1 +mh)(y 1 +mk 1 ) =..4 (.98.9).75 y = y 1 +ak 1 +bk.9 Comparing this to the exact expression we find a relative error 4 1 4. If we instead use the Euler method, which for the present problem becomes: y n+1 = y n hx n y n = (1 hx n )y n, we find: y 1 = (1 hx )y = 1 y = (1 hx 1 )y 1 = (1..) 1 =.96 this gives a relative error 4 1 compared to the analytic solution.. Use the Euler-Maclaurin summation formula to show that n m = 1 n(n+1) and n m = 1 6 n(n+1)(n+1) Solution: Using h = 1 in the Euler-Maclaurin formula gives us f() + n f(m)+ f(n) n = f(m)dm+ c k k=1 with c k = B k (k)! (f(k) (n) f (k) ())
and where B i are the Bernoulli numbers (B = 1, B = 1/6, B 4 = /,...). So in the first sum, we use f(m) = m. The integral gives n / and we need to add f(n)/ = n/. The first derivative of f is constant and all higher derivatives are zero so all c k vanish and the result is n(n+1)/. In the second sum we use f(m) = m. The integral becomes n / and we add f(n)/ = n /. The derivative of f is m and the B = 1/6 so we need to add c 1 = 1/6! (n ), all other c k vanish. The result is n(n+1)(n+1)/6. 4. Calculate the integral 4 dx I = x with the trapezoidal rule for the step sizes.5,.5 och.15. Improve the values by Richardson extrapolation. Solution: The exact answer is ln() ln(1) = ln().69. Discretizing x between the integration limits as x = x n (n =,1,...,N) where x = and x N = 4 and defining f n = f(x n ) the trapezoidal method is: Thus, ( f1 I trapz (h) h +f +f +...+ f ) N. ( f() I trapz (.5) =.5 ( f() I trapz (.5) =.5 +f(.5)+ f(4) ).78, +f(.5)+f(.5)+f(.75)+ f(4) ).697. ( f() I trapz (.15) =.15 +f(.15)+f(.5)+f(.75)+f(.5)+f(.65) +f(.75)+f(.875) + f(4) ).6941. The relative errors compared to the exact result are. and.56 respectively. Using one Richardson extrapolation step we get the improved estimates of the integral I: and Î(.5) = 4I trapz(.5) I trapz (.5).695 Î(.15) = 4I trapz(.15) I trapz (.5).6915 Two Richardson extrapolation steps yields: ˆÎ = 16Î(.15) Î(.5) 15.6915 with a relative error 1 6, i.e. a result very close to the exact one. 5. Calculate the integral in exercise 4 by using two-point Gaussian integration, after a change of variable to t = x 7. Solution: After the suggested change of variables the integral becomes I = dt t+
i.e. on the form given in ODE lectures 4 and 5. Using the explicit formula derived there: 1 I 1/+ + 1 1/+ giving a relative error of.1 compared to the exact result. 6. Determine an integration formula of the type f(x)(1+x )dx A 1 f(x 1 )+A f(x ) that is exact for all polynomials with degree less than 4. Solution: We need the corresponding second order orthogonal polynomial, and we use the Gram-Schmidt method with w(x) = 1+x. As always φ = 1. φ 1 = x φ x φ φ φ = x φ = x φ 1 x φ 1 φ 1 φ 1 φ x φ φ φ = x 5 So the zeros are ± /5. Inserting f(x) = 1 gives (x +1)dx = 8 = A 1 +A and f(x) = x gives So A 1 = A = 4/ and x(x +1)dx = = A 1 ( /5)+A ( /5) f(x)(1+x )dx 4 f( /5)+ 4 f( /5) is exact for all polynomials with degree less than 4. 7. Consider the boundary value problem y (x) y(x) = with y() = and y(1) = sinh(1). What is theanalytic solution? Solve this problemusingthe matrix methodwith a step size h =.5 (this gives -equation system to be diagonalized). Compare your numerical solution to the analytic result. Solution: The analytic solution is: y(x) = sinh(x) Discretizing y (x) y(x) = gives y n+1 (+h )y n +y n for n = 1,,. The boundary condition becomes: y = and y 4 = sinh(1). Or, on matrix form, A y = b, we have (+h ) 1 A = 1 (+h ) 1 1 (+h ) and b = sinh(1) Comparing the solution of this equation system to the exact result we find that the relative errors for x n =.5,.5 and.75 are roughly 7.6 1 4, 6. 1 4 and.4 1 4 respectively.