12.07.2015 Views

Numerical Differentiation and Integration

Numerical Differentiation and Integration

Numerical Differentiation and Integration

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Numerical</strong> <strong>Differentiation</strong> <strong>and</strong> <strong>Integration</strong>Problem Statement: If the values of a function f are given at a few points, say, x 0 , x 1 ,···, x n , we attempt to estimate a derivative f ′ (c) oranintegral ∫ ba f(x)dx.• Basics of <strong>Numerical</strong> <strong>Differentiation</strong>• Richardson Extrapolation• Basics of <strong>Numerical</strong> <strong>Integration</strong>• Quadrature Formulas• Trapezoidal Rule• Simpson’s 1 Rule 3♥ Simpson’s 3 Rule 8♥ Boole’s Rule


Basics of <strong>Numerical</strong> <strong>Differentiation</strong>f ′ (x) = limit h→0f(x + h) − f(x)h(1)f ′ (x) ≈ 1 [f(x + h) − f(x)] (2)hf(x + h) = f(x)+hf ′ (x)+ h22 f ′′ (ξ) (3)f ′ (x) = 1 h [f(x + h) − f(x)] − h 2 f ′′ (ξ) (4)f ′ (x) ≈ 1 [f(x + h) − f(x − h)] (5)2hf(x + h) = f(x)+hf ′ (x)+ h22 f(x)+h3 3! f (3) (ξ) (6)f(x − h) = f(x) − hf ′ (x)+ h2 h3f(x) −2 3! f (3) (τ) (7)f ′ (x) = 1h2[f(x + h) − f(x − h)] −2h 12 [f (3) (ξ)+f (3) (τ))] (8)Examples:1. f(x) =cos(x), evaluate f ′ (x) atx = π/4 withh =0.01, h =0.0052. g(x) =ln(1 + x), evaluate g ′ (x) atx =1withh =0.01, h =0.005.3. t(x) =tan −1 x,evaluatet ′ (x) atx = √ 2withh =0.01, h =0.005.• True Solutions1. f ′ (π/4) = − sin(π/4) = − 1 √2= −0.7071067812. g ′ (1) = 11+1 =0.5000000003. t ′ ( √ 2) =11+( √ 2) 2 = 1 3 =0.333333333


Approximation by Taylor ExpansionTheorem 1: Assume that f ∈ C 3 [a, b] <strong>and</strong>x − h, x, x + h ∈ [a, b] withh>0. Thenf ′ f(x + h) − f(x − h)(x) ≈2hFurthermore, ∃c ∈ [a, b] such that= D 0 (h) (9)f ′ (x) ≈f(x + h) − f(x − h)2h+ E 1 (f,h) (10)where E 1 (f,h) =− h2 f (3) (c)= O(h 2 ) is called the truncation error.6Theorem 2: Assume that f ∈ C 5 [a, b] <strong>and</strong>x ∓ 2h, x ∓ h, x ∈ [a, b] withh>0. Thenf ′ −f(x +2h)+8f(x + h) − 8f(x − h)+f(x − 2h)(x) ≈12hFurthermore, ∃c ∈ [a, b] such that= D 1 (h) (11)f ′ (x) ≈ −f 2 +8f 1 − 8f −1 + f −212h+ E 2 (f,h) (12)where E 2 (f,h) =− h4 f (5) (c)30= O(h 4 )h By Theorem 1 By Theorem 2 Richardson0.1 -0.716161095 -0.7173537030.01 -0.717344150 -0.7173561080.001 -0.717356000 -0.7173561670.0001 -0.717360000 -0.717360833f ′ (0.8) -0.717356091 -0.717356091 -0.717356091Table 1: Approximating the derivative of f(x) =cos(x) atx =0.8


Richardson’s ExtrapolationRecall thatThenf ′ (x 0 ) ≈ D 0 (h)+Ch 2 , f ′ (x 0 ) ≈ D 0 (2h)+4Ch 2 (13)f ′ (x 0 ) ≈ 4D 0(h) − D 0 (2h)3≈ −f 2 +8f 1 − 8f −1 + f −212h= D 1 (h) (14)Similarly,f ′ (x 0 )= −f 2 +8f 1 − 8f −1 + f −212h+ h4 f (5) (ξ)30≈ D 1 (h)+Ch 4 (15)Thenf ′ (x 0 )= −f 4 +8f 2 − 8f −2 + f −412h+ h4 f (5) (τ)30f ′ (x 0 ) ≈ 16D 1(h) − D 1 (2h)15≈ D 1 (2h)+16Ch 4 (16)(17)Richardson’s Extrapolation Theorem: Let D k−1 (h) <strong>and</strong>D k−1 (2h) be two approximationsof order O(h 2k )forf ′ (x 0 ), such thatf ′ (x 0 )=D k−1 (h)+c 1 h 2k + c 2 h 2k+2 + ··· (18)f ′ (x 0 )=D k−1 (2h)+2 2k c 1 h 2k +2 2k+2 c 2 h 2k+2 + ··· (19)Then an improved approximation has the formf ′ (x 0 )=D k (h)+O(h 2k+2 )= 4k D k−1 (h) − D k−1 (2h)4 k − 1+ O(h 2k+2 ) (20)


<strong>Differentiation</strong> Approximation Algorithms• Algorithm: <strong>Differentiation</strong> Using LimitsGenerate the numerical sequencef ′ (x) ≈ D j = f(x +2−j h) − f(x − 2 −j h)2 × (2 −j h)for j =0, 1, ···,nuntil |D n+1 − D n |≥|D n − D n−1 | or |D n − D n−1 |


Matlab Codes for Richardson Extrapolation%% Script file: richardson.m% Example: format long% richardson(@cos,0.8,0.00000001,0.00000001)% richardson(@sinh,1.0,0.00001,0.00001)%% Richardson Extrapolation for numerical differentiation% P.333 of John H. Mathews, Kurtis D. Fink% Input f is the function input as a string ’f’% delta is the tolerance error% toler is the tolerance for the relative error% Output D is the matrix of approximate derivatives% err is the error bound% relerr is the relative error bound% n is the coordinate of the best approximation%function [D, err, relerr, n]=richardson(f,x,delta,toler)err=1.0;relerr=1.0;h=1.0;j=1;D(1,1)=(feval(f,x+h)-feval(f,x-h))/(2*h);while (relerr>toler & err>delta & j


Basics of <strong>Numerical</strong> <strong>Integration</strong>• ∫ ∞01 √2πe −x2 /2 dx = 1 2• ∫ √20 1 − x2 dx = π 4 2• ∫ π0 sin(x)dx =2• ∫ 4 √1 xdx =143• erf(x) = 2 √ π∫ x0 e−t2 /2 dt, 0


Quadrature FormulasThe general approach to numerically compute the definite integral ∫ ba f(x)dx is by evaluatingf(x) at a finite number of sample <strong>and</strong> find an interpolating polynomial to approximatethe integr<strong>and</strong> f(x).Definition: Suppose that a = x 0


Approximating Integrals by Trapezoidal <strong>and</strong>Simpson’s RulesExample: The arc length of a curve f(x) = 2 3 x3/2 between x ∈ [0, 1] can be computed byα =• Trapezoidal Rule∫ 10√1+xdx =23 (2√ 2 − 1) = 1.21895142α ≈ h 2 [f(x 0)+2f(x 1 )+2f(x 2 )+···+2f(x n−1 )+f(x n )]where 0 = x 0


Matlab Codes for Simpson’s 1/3 Rule%% Simpson.m - Simpson’s 1/3 Rule for \sqrt(1+x)%format longn=20;h=1/n;x0=0; x1=x0+h; x2=x1+h;s=0;for i=0:2:n-2,f0=sqrt(1+x0);f1=sqrt(1+x1);f2=sqrt(1+x2);s=s+f0+4*f1+f2;x0=x2;x1=x2+h;x2=x2+h+h;ends=h*s/3.0;’Simpson Approximated Arc length is’, s

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!