Numerical methods - lecture 9 Jiří Zelinka Autumn 2017 Jiří Zelinka Numerical methods - lecture 9 1/16 Numerical calculation of the derivative xq, ..., xn - given points, fo,...,fn- given function values, = f(*k) We want to calculate the approximation of f\x) from this data. Let P be the interpolation polynomial for given data. f'{x) « P'(x) Example 1. n = l, Data: x0,xi, ŕ0, fľ Pi*) = *E*;(x - xo) + fo f'(x) « P'(x) = xi—xq Jiří Zelinka Numerical methods - lecture 9 2/16 Example 2. n = 2, data: x0,xi,x2, ŕ0, /i, £ P(x) = ŕ (x-xi)(x~x2) i f (x-xq)(x-x2) i f (x-x0)(x-xi) v ' 0 (x0-xi)(x0-x2) 1 (xi-x0)(xi-x2) 2 (x2-x0)(x2-xi) pf(x) = ŕ 2x-xi~x2 i f 2x-xp-x2 i ^ 2x-x0-xi v ' 0 (x0-xi)(x0-x2) 1 (xi-x0)(xi-x2) 2 (x2-x0)(x2-xi) Equidistant points: xi — x0 = x2 — xi = h : P'(v\ _ f 2x-xi-x2 f 2x-x0-x2 i r 2x-x0-xi Př(x2) = ±(f0-4f1 + 3f2) P"(x) = ±(f0-2f1 + f2) Jiří Zelinka Numerical methods - lecture 9 3/16 Derivation from the Taylor series / : f(x+h) = f{x) + f'{x)h + \f"{x)h2 + \f'"{x)tf + 0(h4) II : f(x-h) = f{x) - f'\x)h + \f"\x)h2 - ±f'"{x)h3 + 0(/j4) /-// : f{x+h) - f(x-h) = 2f'{x)h + |P'(x)/73 + 0(/74) f'{x) = i;[f(x+h)-f{x-h)] + 0(ri>) l+ll : f(x+h) + f{x-h) = 2f{x) + f"{x)h2 + 0{h4) f»(x) = ±[f(x+h) - 2f(x) + f(x-h)] + 0{h2) 4/16 Numerical integration - quadrature formulae x0,..., xn - given points, a < x0 < x\ < • • • < xn < b fo,...,fn- given function values, = f{xi<) Let P be the interpolation polynomial for given data. f(x)dx& / P(x)dx Example 1. n = 1, a = x0, b = xi, ŕ(a), P(x) = M(x_a) + f(a) JP(x)dx=\f^ _ f(a)+f(b) (b-a) □ S1 Jiří Zelinka Numerical methods - lecture 9 5/16 Trapezoidal rule Jiří Zelinka 6/16 Example 2. n = 2, equidistant points: a = x0,xi = a + h = ä4^, b = x2 = a + 2h, ^b? fi? 6 ~ function values Pfx) = ŕ (x~xi)(x~x2) i f (x-xq)(x-x2) i f (x-x0)(x-xi) V / 0 ( Yn — Vi Vxn — ^" (*1—Xq)(xi—x2 ) ^ f yv,— YnV Yo — (x0-xi)(x0-x2) b b (x2-x0)(x2-x1) J f{x)dx ^ J P{x)dx = ^ [f{a) + 4f{^) + f{b) = l[f(a) + 4f(?¥) + f(b)] Jiří Zelinka Numerical methods - lecture 9 7/16 Jiří Zelinka Numerical methods - lecture 9 Composite (chained) trapezoidal rules Equidistant points: a = x0 < xi < • • • < b = xm X/+11 = x/ + h, ft = f (x,) We use the trapezoidal rule for every interval [x;,x;+i]: —r—n H---—h H---—h H-----|----h = -[fo + 2f1 + 2f2 + --- + 2fn_1 + fn] Jiří Zelinka Numerical methods - lecture 9 9/16 Composite Simpson's rules Equidistant points, n - even: a = x0 < xi < • • • < b = xm X/+11 = x/ + h, ft = f (x,) We use the Simpson's rule for every interval [x2/,x2/+2] ^ [f0 + 4/i + f2] + ^ [6 + 4/3 + + • • • + +^ [f„-2 + 4/n-l + /n] = ^[fo + 4/i + 2f2 + 4/3 + 2/4 + • • • + 2fn_2 + 4/:n_i + fn] 11/16 350 r 300 ■ 0 1-.............-1 0 2 4 6 8 10 x Jiří Zelinka 12/16 Monte Carlo integration Method I Xi,... ,Xn - random numbers distributed uniformly on [a, b] b J f(x)dx « ^ f(X;) 13/16 Monte Carlo integration Method II Let f be non-negative on [a, b], f{x) < M for every x G [a, b]. Pd5 Vi]f... ,[X„. Y„] - observations of the random vector [X, Y] distributed uniformly on [a, b] x [0, M] b J f{x)dx 1 n P(Y+- +" ,+ + + ^ ++^ + "n- -+ #+ Í+ + + +" + + V+* + ±1^# ++* + ++ + + t+i ++ +^ 0 r++" -H--U ■ +^-+, -u" -n- iL ++ o 0.2 0.4 0.6 0.8 1 .2 1 .4 1 .6 1 .8 15/16 Application: Approximation of tt: [X, Y] distributed uniformly on [0,1] x [0,1] P(X2 + Y2 < 1) = f [Xi, Vi],... \Xn. Yn\\ observations of [X, Y] n ;'=1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 □ S1 Jiří Zelinka Numerical methods - lecture 9 16/16