SF1544 Övning 3 November 15, 2016 1 / 16 This övning Numerical integration (Numerisk integrering) Composite trapezoidal rule (Trapetsmetoden) Numerical differentiation (Differensoperator) November 15, 2016 2 / 16 Numerisk integrering Let a = x1 < x2 < · · · < xn−1 < xn = b then we approximate Z b f (x)dx ≈ a n X wi f (xi ) i=1 November 15, 2016 3 / 16 Numerisk integrering Let a = x1 < x2 < · · · < xn−1 < xn = b then we approximate Z b f (x)dx ≈ a n X wi f (xi ) i=1 Examples Mid point rule Z b f (x)dx ≈ (b − a)f a a+b 2 November 15, 2016 3 / 16 Numerisk integrering Let a = x1 < x2 < · · · < xn−1 < xn = b then we approximate Z b f (x)dx ≈ a n X wi f (xi ) i=1 Examples b Z Mid point rule f (x)dx ≈ (b − a)f a Trapezoidal rule Z a b a+b 2 f (a) + f (b) f (x)dx ≈ (b − a) 2 November 15, 2016 3 / 16 Illustration of composite trapezoidal November 15, 2016 4 / 16 Trapezoidal rule Z a b N−1 1X (xi+1 − xi ) [f (xi+1 ) + f (xi ))] f (x)dx ≈ 2 i=1 November 15, 2016 5 / 16 Trapezoidal rule Z a b N−1 1X (xi+1 − xi ) [f (xi+1 ) + f (xi ))] f (x)dx ≈ 2 i=1 If xi+1 = xi + h (uniform grid) Z b a Z a "N−1 # N−1 X h X f (x)dx ≈ f (xi+1 ) + f (xi ) 2 i=1 b i=1 # N−1 f (a) + f (b) X f (x)dx ≈ h + f (xi ) 2 " i=2 November 15, 2016 5 / 16 Exercise Approximate with trapezoidal rule and n = 5 Z 1 x 2 dx 0 November 15, 2016 6 / 16 Exercise Approximate with trapezoidal rule and n = 5 Z 1 x 2 dx 0 We have h = 0.25 and x1 = 0, x2 = 0.25, x3 = 0.5, x4 = 0.75, x5 = 1 November 15, 2016 6 / 16 Exercise Approximate with trapezoidal rule and n = 5 Z 1 x 2 dx 0 We have h = 0.25 and x1 = 0, and then Z 1 0 x2 = 0.25, x3 = 0.5, x4 = 0.75, x5 = 1 f (0) + f (2) f (x)dx ≈ 0.25 + f (0.25) + f (0.5) + f (0.75) 2 November 15, 2016 6 / 16 Exercise Approximate with trapezoidal rule and n = 5 Z 1 x 2 dx 0 We have h = 0.25 and x1 = 0, and then Z 1 0 Z 0 x2 = 0.25, x3 = 0.5, x4 = 0.75, x5 = 1 f (0) + f (2) f (x)dx ≈ 0.25 + f (0.25) + f (0.5) + f (0.75) 2 1 02 + 22 2 2 2 x dx ≈ 0.25 + 0.25 + 0.5 + 0.75 = 0.34375 2 2 November 15, 2016 6 / 16 Exercise Approximate with trapezoidal rule and n = 5 Z 1 1 x 2 dx = = 0.3̄ 3 0 We have h = 0.25 and x1 = 0, and then Z 1 0 Z 0 x2 = 0.25, x3 = 0.5, x4 = 0.75, x5 = 1 f (0) + f (2) f (x)dx ≈ 0.25 + f (0.25) + f (0.5) + f (0.75) 2 1 02 + 22 2 2 2 x dx ≈ 0.25 + 0.25 + 0.5 + 0.75 = 0.34375 2 2 November 15, 2016 6 / 16 Matlab implementation function [ res ] = trapez(f, a, b, n ) %TRAPEZ composite trapezoidal rule x=linspace(a,b,n); res=f(a)+f(b); for i=2:n-1 res=res+2*f(x(i)); end h=x(2)-x(1); res=res*h/2; end November 15, 2016 7 / 16 MATLAB DEMO November 15, 2016 8 / 16 Exercise from the book November 15, 2016 9 / 16 Solution We have N = 9 points Z 0 1 # 8 B(0) + B(1) X B(x)dx ≈ h + B(xi ) 2 " i=2 November 15, 2016 10 / 16 Solution We have N = 9 points Z 0 1 # 8 B(0) + B(1) X B(x)dx ≈ h + B(xi ) 2 " i=2 and h = 0.125, November 15, 2016 10 / 16 Solution We have N = 9 points Z 0 1 # 8 B(0) + B(1) X B(x)dx ≈ h + B(xi ) 2 " i=2 and h = 0.125, x1 = 0, x2 = 0.1250, x3 = 0.2500, x4 = 0.3750, x5 = 0.5000, x6 = 0.6250, x7 = 0.7500, x8 = 0.8750 x9 = 1.0000 November 15, 2016 10 / 16 Solution We have N = 9 points Z 0 1 # 8 B(0) + B(1) X B(x)dx ≈ h + B(xi ) 2 " i=2 and h = 0.125, x1 = 0, x2 = 0.1250, x3 = 0.2500, x4 = 0.3750, x5 = 0.5000, x6 = 0.6250, x7 = 0.7500, x8 = 0.8750 Z 0 1 x9 = 1.0000 2.60 + 0.92 B(x)dx ≈0.125 + 2.08 + 1.72 + 1.45 + 1.26 2 + 1.13 + 1.04 + 0.97 = 1.426 November 15, 2016 10 / 16 Numerical differentiation Recall f (x + h) − f (x) h→0 h f 0 (x) = lim Then we can approximate for h small forward difference formula f 0 (x) ≈ f (x + h) − f (x) h h small November 15, 2016 11 / 16 Example Let f (x) = log(x), approximate f 0 (1). f 0 (1) ≈ log (1 + h) f (1 + h) − f (1) = h h November 15, 2016 12 / 16 Example Let f (x) = log(x), approximate f 0 (1). f 0 (1) ≈ log (1 + h) f (1 + h) − f (1) = h h For h = 0.1 f 0 (1) ≈ log (1.1) = 0.9531 0.1 November 15, 2016 12 / 16 Example Let f (x) = log(x), approximate f 0 (1). f 0 (1) ≈ log (1 + h) f (1 + h) − f (1) = h h For h = 0.1 f 0 (1) ≈ log (1.1) = 0.9531 0.1 f 0 (1) ≈ log (1.01) = 0.9950 0.01 For h = 0.01 November 15, 2016 12 / 16 Example Let f (x) = log(x), approximate f 0 (1). f 0 (1) ≈ log (1 + h) f (1 + h) − f (1) = h h For h = 0.1 f 0 (1) ≈ log (1.1) = 0.9531 0.1 f 0 (1) ≈ log (1.01) = 0.9950 0.01 f 0 (1) ≈ log (1.001) = 0.9995 0.001 For h = 0.01 For h = 0.001 November 15, 2016 12 / 16 Example Let f (x) = log(x), approximate f 0 (1). f 0 (1) ≈ log (1 + h) f (1 + h) − f (1) = h h For h = 0.1 f 0 (1) ≈ log (1.1) = 0.9531 0.1 f 0 (1) ≈ log (1.01) = 0.9950 0.01 For h = 0.01 For h = 0.001 log (1.001) = 0.9995 0.001 and so f 0 (1) = 1 f 0 (1) ≈ Correct value: f 0 (x) = 1 x November 15, 2016 12 / 16 Exercise Prove that f 0 (x) = −f (x + 2h) + 4f (x + h) − 3f (x) + O(h2 ) 2h November 15, 2016 13 / 16 Exercise Prove that f 0 (x) = −f (x + 2h) + 4f (x + h) − 3f (x) + O(h2 ) 2h Solution: TAYLOR EXPANSION November 15, 2016 13 / 16 Taylor expansion Recall f (x + ∆x) = f (x) + ∆xf 0 (x) + ∆x m ∆x 2 00 f (x) + · · · + f (m) (x) + O(∆x m+1 ) 2 m! November 15, 2016 14 / 16 Taylor expansion Recall f (x + ∆x) = f (x) + ∆xf 0 (x) + ∆x m ∆x 2 00 f (x) + · · · + f (m) (x) + O(∆x m+1 ) 2 m! In a compact form f (x + ∆x) = m X i=0 f (i) (x) ∆x i + O(∆x m+1 ) i! November 15, 2016 14 / 16 Exercise Prove that f 0 (x) = −f (x + 2h) + 4f (x + h) − 3f (x) + O(h2 ) 2h November 15, 2016 15 / 16 Exercise Prove that f 0 (x) = −f (x + 2h) + 4f (x + h) − 3f (x) + O(h2 ) 2h Solution: TAYLOR EXPANSION November 15, 2016 15 / 16 Solution With ∆x = 2h and m = 2 we have f (x + 2h) = f (x) + 2hf 0 (x) + f 00 (x) 4h2 + O(h3 ) 2 November 15, 2016 16 / 16 Solution With ∆x = 2h and m = 2 we have f (x + 2h) = f (x) + 2hf 0 (x) + f 00 (x) 4h2 + O(h3 ) 2 With ∆x = h and m = 2 we have f (x + h) = f (x) + hf 0 (x) + f 00 (x) h2 + O(h3 ) 2 November 15, 2016 16 / 16 Solution With ∆x = 2h and m = 2 we have f (x + 2h) = f (x) + 2hf 0 (x) + f 00 (x) 4h2 + O(h3 ) 2 With ∆x = h and m = 2 we have f (x + h) = f (x) + hf 0 (x) + f 00 (x) h2 + O(h3 ) 2 With ∆x = 0 we have f (x) = f (x) November 15, 2016 16 / 16 Solution With ∆x = 2h and m = 2 we have f (x + 2h) = f (x) + 2hf 0 (x) + f 00 (x) 4h2 + O(h3 ) 2 With ∆x = h and m = 2 we have f (x + h) = f (x) + hf 0 (x) + f 00 (x) h2 + O(h3 ) 2 With ∆x = 0 we have f (x) = f (x) With a direct computation −f (x + 2h) + 4f (x + h) − 3f (x) = f 0 (x) + O(h2 ) 2h November 15, 2016 16 / 16
© Copyright 2026 Paperzz