Chapter 6 - Numerical Differentiation & Integration
Chapter 6 - Numerical Differentiation & Integration
Chapter 6 - Numerical Differentiation & Integration
Last lecture:
Least square approximation using common orthogonal polynomials
This lecture:
Numerical differentiation
Numerical integration
CHAPTER VI NUMERICALDIFFERENTIATION INTEGRATION
2! n!
◊ Consider (xi-1, xi, xi+1) with spacing h = xi - xi-1.
xi-1 xi+1
h2 h3
f ( xi +1 ) = f ( xi + h) = f ( xi ) + hf ( xi ) + f ( xi ) + f ( xi ) + .... (1)
x 2! 3!
a=xi h 2
h3
f ( xi −1 ) = f ( xi − h) = f ( xi ) − hf ( xi ) + f ( xi ) − f ( xi ) + .... (2)
2! 3!
❑ First order derivatives fi"
ℎ2 ″ ℎ3
fi-1 = fi - fi' h + 𝑓 - 3! fi′ (2)
2!
Example
* f(x): fi-2 = 0.216, fi-1 = 0.512, fi =1, fi+1 = 1.728. * Second order one-side difference:
3𝑓𝑖 −4𝑓𝑖−1 +𝑓𝑖−2 3−4∗0.512+0.216
* Exact derivatives: fi' = 3xi2 = 3; fi" = 6, fi"' = 6. f i' = = = 2.92
2ℎ 2∗0.2
𝑓𝑖+1 −𝑓𝑖 1.728−1
* Forward difference: f i' = = = 3.6 Error = T.V. - A.V. = 3.0 – 2.92 = 0.08 (actual);
ℎ 0.2
Error = T.V. - A.V. = 3 - 3.64 = -0.64 1
T.E. ~ 𝑓 ″′ (𝑥𝑖 ) ℎ2 = 0.08 (estimate)
3
1
T.E.~ − 2 𝑓 ″ (𝑥𝑖 ) ℎ = -3*0.2 = -0.6
* Second order derivative: exact f" = 6x, fi"= 6
𝑓𝑖 −𝑓𝑖−1 1−0.512
* Backward difference: : f i' = = =2.44 fi" =
𝑓𝑖+1 −2𝑓𝑖+𝑓𝑖−1 1.728−2+0.512
=6
ℎ 0.2 ℎ2 0.22
Error = T.V. - A.V. = 3-2.44 = 0.56 (actual);
1 ″ Error = 6 - 6 = 0. Why?
T.E.~ 2
𝑓 (𝑥𝑖 ) ℎ = 3*0.2 = 0.6 1
T.E. ~ - 12 𝑓 (4) (𝑥𝑖 ) ℎ2 = 0 (estimate)
Lecture 25
Last lecture:
Numerical differentiation
This lecture:
* Let s = (x-x0)/h x = h
x
x-xj = [x0+hs – (x0+jh)] = (s-j)h
& dx = h ds, a=x0 xi-2 xi-1 xi xi+1 xi+2 b= xn
x =[a, b] s =[0, n]
𝑛 𝑠 𝑠−1 𝑠−2 ... 𝑠−𝑛
Integration error = hn+2 0 f(n+1)(x) ds
(𝑛+1)!
6.2.2 Trapezoidal rule (n=1)
𝑥1 1 y f1
I1 = 𝑥P1(x)dx =0 [f0 + s(f1−f0)] hds
0 f(x) P1(x)
* Shaded area = error
I1 = h(f0+ f1)/2 --Trapezoidal rule.
f0 in trapezoidal rule.
𝑥
* Integration Error = 𝑥1 (interpolation error) dx * Error curvature.
0
1
= h3 0 s(s−1)/2 f"(x) ds
1 1
= f"(x) h3 [ 3 s3 − s2]10
2 x0 x1 x
1
= - 12 f"(x) h3
6.2.3 Simpson’s rule (n=2)
1 1 1 1
I2 = h [sf0 + 2 𝑠2f0+ 2f0 2(3 𝑠3- 2 𝑠2)]20
1
= h [2f0 + 2f0+ 2f0]
3
3
I3 = 8 h(f0+ 3f1 + 3f2 + f3) -- Simpson’s 3/8 rule. 3/8 rule (n=3) is often used in conjunction with
* Integration Error composite Simpson’s 1/3 rule (n=2)
𝑥
Error = E3 = 𝑥3 (x−x0) (x−x1)(x−x2))(x−x3)f[x0, x1, x2, x3, x] dx when the total number of intervals is odd.
0
◊ The integration formula based on Pn(x) =>
After a very lengthy derivation
Newton-Cotes formula
1 3
E3 =... = 4! h5 f(4)(x) 0 𝑠(𝑠 − 1)(𝑠 − 2)(𝑠 − 3)ds ◊ There are NO practical needs to use higher
3 order polynomials due to increasing roundoff
= - 80 h5 f(4)(x) error associated with large n.
1
• Compare w/ E2 = - 90h5 f(4)(x), There are other ways to improve.
E3 has the same order and larger coefficient.
Lecture 26
Last lecture:
Newton-Cotes integration formula
--numerical integration formula
--integration error
This lecture:
Composite integration
6.2.4 Simpson’s 3/8 rule (n=3)
3
I3 = 8 h(f0+ 3f1 + 3f2 + f3) -- Simpson’s 3/8 rule. 3/8 rule (n=3) is often used in conjunction with
* Integration Error composite Simpson’s 1/3 rule (n=2)
𝑥
Error = E3 = 𝑥3 (x−x0) (x−x1)(x−x2))(x−x3)f[x0, x1, x2, x3, x] dx when the total number of intervals is odd.
0
◊ The integration formula based on Pn(x) =>
After a very lengthy derivation
Newton-Cotes formula
1 3
E3 =... = 4! h5 f(4)(x) 0 𝑠(𝑠 − 1)(𝑠 − 2)(𝑠 − 3)ds ◊ There are NO practical needs to use higher
3 order polynomials due to increasing roundoff
= - 80 h5 f(4)(x) error associated with large n.
1
• Compare with E2 = - 90h5 f(4)(x) There are other ways to improve.
E3 has the same order and larger coefficient.
6.2.5 Mid-point rule (or rectangle rule)
f1
◊ What happens if n=0? y
f1/2 f(x)
* Consider a 0th order polynomial for y = f(x) in [x0, x1]
P0(x)
* It is approximated by a constant value y =P0(x) = f1/2= f(x1/2)
at x =x1/2 (mid point between x0 & x1). f0
* Obviously, the integration gives
I0 = h f1/2 -- rectangle rule.
𝑥
* Integration Error: E0 = 𝑥1 [f(x) − f1/2] dx x0 x1/2 x1 x
0
= h [f(x1/2) + f(x3/2) + f(x5/2)+…+ f(xn-1/2)] * Question: what happens to periodic f '(x) with f '(b)= f '(a)?
Example:
1 1
Evaluate I = 0 𝑑𝑥 using rectangle rule with 2, 4, & 8 intervals
1+𝑥 2
Exact value: Iexact = tan-11 = p/4 = 0.7853981634…
Solution:
i) 2 sub-intervals: h = 0.5; iii) 8 intervals: h = 0.125
x = [0, 0.5] & x = [0.5, 1];
I0 = 0.5×[ f(0.25) + f(0.75)] x x1/2 f(x1/2) Integration Error
0 0.0625 0.996108949
= 0.5×(0.941176 + 0.64) = 0.7905882; 0.125 0.1875 0.966037736
0.25 0.3125 0.911032028
Error(h=0.5) = -0.00519 0.375 0.4375 0.839344262
ii) 4 sub-intervals: h = 0.25, 0.5 0.5625 0.759643917
0.625 0.6875 0.679045093
I = 0.25×(0.984615385 + 0.876712329 0.75 0.8125 0.602352941
0.875 0.9375 0.532224532
+ 0.719101124 + 0.566371681)
1 0.78572368 -0.0003255
= 0.78670013 Error(h=0.25) = -0.001302
• Each time as h is reduced by 1/2, error is reduced by (1/2)2, as
expected for a 2nd order accurate method.
6.2.2 Composite trapezoidal rule
ℎ
= (𝑓0 + 2𝑓1 + 2𝑓2 +. . . 2𝑓𝑛−1 + 𝑓𝑛 ) Or more precisely,
2
1 𝑥
ℎ E1= -
1 2
ℎ ℎ σ𝑛𝑖=1 𝑓"( 𝜉𝑖 ) = - 12 ℎ2 𝑥𝑑)𝑥("𝑓 𝑛 𝑥
= (𝑓 + 𝑓𝑛 ) + ℎ σ𝑛−1
𝑖=1 𝑓𝑖 12 0
2 0
ℎ2
* Integration error: ~- [ f ′(b)− f ′(a)]
12
1
A sub-interval contributes: -12 ℎ3 f "(xi) for xi ≤ xi ≤xi+1. * If f'(x) is singular near a or b, the error will be much larger.
* Sum n such sub-intervals * If f'(b)=f'(a), [f'(x) is periodic in (a, b)] much smaller error.
Lecture 27
Last lecture:
Composite rectangle rule & trapezoidal rule
This lecture:
Romberg integration
(--Richardson extrapolation)
Composite Simpson’s rule
Example:
1 1
Evaluate I = 0 𝑑𝑥 using trapezoidal rule with 2, 4, & 8 intervals
1+𝑥 2
Exact value: Iexact = tan-11 = p/4 = 0.7853981634…
* Now use h′ =h/2, apply the composite trapezoidal rule: h/2 h/2
x
Ih/2 = σ2𝑛
𝑥𝑖
𝑖=1 𝑥𝑖−1 𝑓(𝑥)𝑑𝑥
a=x0 x1 x2 x3 x4 x5 x2n -1 x2n=b
1
Error(h/2) ~ − 12 (ℎ/2)2 [𝑓′(𝑏) − 𝑓′(𝑎)] ~ ¼* Error(h)
1
Since Ih = Iexact - Error(h) ~ Iexact + 12 ℎ2 [𝑓′(𝑏) − 𝑓′(𝑎)]
1
Ih/2 = Iexact - Error(h/2) ~ Iexact + 12 (ℎ/2)2 [𝑓′(𝑏) − 𝑓′(𝑎)]
2 1
Perform Romberg integration for I= 0 𝑑𝑥 using rectangle rule with 8, 16, 32, & 64 intervals
1+𝑥 2
Soln. Note: Iexact = tan-1(2) = 1.10714871779409…
8 1.10631663171835
3.116E-07 =(22x1 1.10694046254292- 1.10631663171835)/(22-1)
16 1.10694046254292 1.10714840615111
1.952E-08
32 1.10709663934290 1.10714869827623
1.220E-09 (1)
64 1.10713569726592 1.10714871657359 Apply another extrapolation to 𝐼ሜ𝑛 now:
(2)
n E(n) E(n/2)/E(n) 𝐼ሜ16 =(24x1.10714869827623-1.10714840615111)/(24-1)
8 8.32086E-04 =1.10714871775124 Error= 4.286E-11
16 2.08255E-04 3.996
(2)
32 5.20785E-05 3.999 𝐼ሜ32 =(24x1.10714871657359-1.10714869827623)/(24-1)
64 1.30205E-05 4.000
=1.10714871779342 Error= 6.719E-13
WHY/HOW does the idea work?-- Romberg integration
(1) 𝒅
I ~ 𝐼ሜ2𝑛 = (22 𝐼2𝑛 − 𝐼𝑛 )/(22 − 1) − 𝟒𝟒 +. . .
𝟒𝒏
Another extrapolation removes O(n-4) term and gives
′
(2) (1) (1) 𝒅
𝐼ሜ2𝑛 = (24 𝐼ሜ2𝑛 − 𝐼ሜ𝑛 )/(24 − 1) − 𝟔𝟔 + …
𝒏
Romberg integration
(𝑘)
Repeat application of this Richardson extrapolation Romberg integration 𝐼ሜ2𝑛
This procedure will improve the accuracy at little extra cost.
(1) (2) (3)
n In 1st extrap. 𝐼ሜ2𝑛 𝐼ሜ2𝑛 𝐼ሜ2𝑛
8 1.10631663171835
16 1.10694046254292 1.10714840615111
32 1.10709663934290 1.10714869827623 1.10714871775124
64 1.10713569726592 1.10714871657359 1.10714871779342 1.10714871779409
Note: Iexact = 1.10714871779409…
General procedure:
• obtain In using trapezoidal rule; step size h=(2-0)/n = 2/n
(1)
• obtain the 1st extrapolation as 𝐼ሜ2𝑛 =(22I2n-In)/(22 -1).
(2) (1) (1)
• obtain the 2nd extrapolation as 𝐼ሜ2𝑛 = (24 𝐼ሜ2𝑛 - 𝐼ሜ𝑛 )/(24 -1).
(3) (2) (2)
• obtain the 3rd extrapolation as 𝐼ሜ2𝑛 = (26 𝐼ሜ2𝑛 - 𝐼ሜ𝑛 )/(26 -1).
6.3.4 Composite Simpson's Rule
x3 x4 h
h= Dx= (b - a)/n. a=x0 x1 x2 xn -2 xn -1 xn=b
2 1
Compute I = 0 𝑑𝑥 using Simpson's rule & n=8, 16, 32, & 64
1+𝑥 2
Richardson extrapolation)
Gauss quadrature
Example:
2 1
Compute I = 0 𝑑𝑥 using Simpson's rule & n=8, 16, 32, & 64
1+𝑥 2
Solution:
Asymptotic error of integration:
I- I(n) ~ cn-p (3)
p=4 for present problem;
2
but p<4 for I = 0 𝑥𝑑𝑥 using Simpson's rule
1 1
Examine T.E. in I = 0 𝑑𝑥 using Simpson's rule with n=8 & 16
1+𝑥 2
I exact = 0.785398163397448
Iextrap = (R In – In/2)/(R-1)
Solution: For h=0.125 and h=0.0625,
(1)
h I(h) Error E(h) E(h)/E(h/2) R Iextrap =𝐼ሜℎ Error
0.125 0.785398125614677 3.778E-8 16 0.785398165285641 -1.8882E-09 −1.8882E−09 ~ 3.2
5.912E−10
0.0625 0.785398162806206 5.912E-10 63.904 64 0.785398163396548 9.0017E-13
0.03125 0.785398163388209 9.2393E-12
Still use incorrect R=16:
Note: as h decreases by a factor of 2, the error is supposed to (1) (2)
h 1st Iextrap = 𝐼ሜℎ 2nd Iextrap = 𝐼ሜℎ
decrease by a factor of 24=16. 0.125
Here, the error actually decreases by a factor of almost 64. 0.0625 0.785398165285641
0.03125 0.785398163427009 0.785398163397507
Why?
Error=
-2.9561E-11 Error= 5.873E-14
Reason: f(3)(0)= f(3)(1)=0 so that T.E. on the O(h4) level is 0.
The T.E. must come from the effects of next higher −2.9561E−11
= 3.20
9.2393E−12
order, which is O(h6). Thus, E(h)/E(h/2)=26=64.
Richardson Extrapolation
1
Use Simpson's rule for I = 0 𝑒 𝑥 𝑑𝑥 and assess errors.
Iexact = e –1 =1.718281828459045
• Newton-Cotes formulae used fixed coordinates, x0, x1, ..., xn, to integrate.
• How about letting xi’s be free to choose too so that we have more 𝑦
flexibility to eliminate higher order T.E. & get better accuracy?
1 1
Evaluate I = 0 𝑑𝑥 using 2-point Gauss quadrature
1+𝑥 2
Solution: a=0, b=1, x = (b+a)/2 + (b-a)t/2 = 0.5+ 0.5 t; dx = dt/2
* Consider
1
𝐼 = −1 f(t)dt = σ𝑛𝑖=1 𝑤𝑖 𝑓(𝑡𝑖 ) (2)
1 0, 𝑘 𝑜𝑑𝑑 𝑛 0, 𝑘 𝑜𝑑𝑑
න tkdt =ቐ 2 𝑤𝑖 𝑡𝑖𝑘 =ቐ 2
−1
, 𝑘 𝑒𝑣𝑒𝑛 𝑖=1 , 𝑘 𝑒𝑣𝑒𝑛
𝑘+1 𝑘+1
◊ There is an alternative derivation for (ti, wi) based on the roots of Legendre polynomials.
Nodes & weights for N-term Gauss-Legendre quadrature
ti wi
◊ n=3: ◊ n=12:
-0.774596669241483 0.555555555555556
-0.981560634246732 0.047175336386475
0 0.888888888888889
-0.904117256370452 0.106939325995363
0.774596669241483 0.555555555555556
-0.769902674194317 0.160078328543358
◊ n=4: -0.587317954286614 0.203167426723067
-0.367831498998180 0.233492536538353
-0.861136311594052 0.347854845137454
-0.125233408511468 0.249147045813402
-0.339981043584856 0.652145154862546
0.125233408511468 0.249147045813402
0.339981043584856 0.652145154862546
0.367831498998180 0.233492536538353
0.861136311594052 0.347854845137454
0.587317954286614 0.203167426723067
◊ n=5: 0.769902674194317 0.160078328543358
-0.906179845938664 0.236926885056189 0.904117256370452 0.106939325995363
-0.538469310105683 0.478628670499366 0.981560634246732 0.047175336386475
0 0.568888888888889
0.538469310105683 0.478628670499366
0.906179845938664 0.236926885056189
◊ n=6:
-0.932469514203152 0.171324492379170
-0.661209386466264 0.360761573048137
-0.238619186083196 0.467913934572691
0.238619186083196 0.467913934572691
0.661209386466264 0.360761573048137
0.932469514203152 0.171324492379170
6.4.3 Gaussian quadrature with different weighting functions w(x)
N-term quadrature:
2
න f(x) e−x dx = σ𝑛𝑘=1 lk 𝑓(xk) (3)
−
Expect: I=Iexact for f(x) = pn(x) = polynomial of degree n (2N-1).
Since 2 2𝑚−1 !! 2
න 𝑥 2𝑚 e−x dx = 2𝑚
𝜋, − 𝑥 2𝑚−1 e−x dx = 0
−
* n=3: * n=8: xk lk
xk lk
1.2247448713915891 0.295408975150919 2.930637420257240 1.99604072211367E-04
0 1.18163590060368 1.981656756695840 1.70779830074134E-02
1.157193712446780 2.07802325814891E-01
* n=4: 0.381186990207322 6.61147012558241E-01
1.65068012388579 0.08131283544724518
0.524647623275290 0.8049140900055128
* n=128:
xk lk -6.34251881700178E+00 7.22010731692829E-19
-1.52918197668827E+01 1.79906598010928E-102 -6.12979658942216E+00 1.01893323042329E-17
-1.47338424735893E+01 2.60817240240911E-95 -5.91833117508581E+00 1.29454815933937E-16
-1.42739813047878E+01 1.40468977131508E-89 -5.70805150876808E+00 1.48422383751385E-15
-1.38652069844762E+01 1.26124948333853E-84 -5.49889066897391E+00 1.53904973035354E-14
-1.34895564126231E+01 3.40123008693663E-80 -5.29078548147718E+00 1.44634732119041E-13
-1.31377747880276E+01 3.75121586880472E-76 -5.08367616748934E+00 1.23421448660055E-12
-1.28043120820671E+01 2.04158579724398E-72 -4.87750603026481E+00 9.58031650873586E-12
-1.24855125853494E+01 6.21424416183031E-69 -4.67222117493263E+00 6.77578048777455E-11
-1.21788086198312E+01 1.15615516409637E-65 -4.46777025714858E+00 4.37318665984840E-10
-1.18823101188783E+01 1.40446725774048E-62 -4.26410425682551E+00 2.57939722942639E-09
-1.15945750547414E+01 1.17197850121298E-59 -4.06117627374927E+00 1.39219071529351E-08
-1.13144716442899E+01 6.99307292405195E-57 -3.85894134234428E+00 6.88458112215435E-08
-1.10410909760196E+01 3.08207738333929E-54 -3.65735626323530E+00 3.12287298617890E-07
-1.07736891151614E+01 1.03048625205569E-51 -3.45637944957173E+00 1.30074700323819E-06
-1.05116473299148E+01 2.67274375173606E-49 -3.25597078635065E+00 4.97992453259098E-06
-1.02544439284709E+01 5.48021702897879E-47 -3.05609150120268E+00 1.75404858480939E-05
-1.00016337989301E+01 9.02804013878664E-45 -2.85670404529740E+00 5.68874376004024E-05
-9.75283321343917E+00 1.21177953413059E-42 -2.65777198318948E+00 1.70014088262809E-04
-9.50770832327906E+00 1.34149748176436E-40 -2.45925989056573E+00 4.68551537808411E-04
-9.26596630029618E+00 1.23808555797636E-38 -2.26113325897306E+00 1.19156381445716E-03
-9.02734841339479E+00 9.61670806796750E-37 -2.06335840670856E+00 2.79783940160579E-03
-8.79162454488869E+00 6.33991352636649E-35 -1.86590239514059E+00 6.06886240692588E-03
-8.55858879506451E+00 3.57437889587942E-33 -1.66873294980372E+00 1.21669188644693E-02
-8.32805592079015E+00 1.73510302028206E-31 -1.47181838567448E+00 2.25543101678244E-02
-8.09985842150790E+00 7.29654500676840E-30 -1.27512753608915E+00 3.86739548106369E-02
-7.87384413353543E+00 2.67292362005807E-28 -1.07862968481090E+00 6.13607210044900E-02
-7.64987422768100E+00 8.57283048376936E-27 -8.82294500792981E-01 9.01086783764489E-02
-7.42782152995230E+00 2.41840345964766E-25 -6.86091975217334E-01 1.22503273164135E-01
-7.20756910338733E+00 6.02598403200645E-24 -4.89992360415458E-01 1.54210435298354E-01
-6.98900904264477E+00 1.33136785903359E-22 -2.93966110300295E-01 1.79773083907799E-01
-6.77204144325592E+00 2.61745758393481E-21 -9.79838219558190E-02 1.94097611864087E-01
-6.55657351526448E+00 4.59400767732972E-20 … …
Gaussian-Chebyshev quadrature
Evaluate:
1 f(x)
𝐼 = −1 dx accurately and efficiently
1−𝑥 2
p p
Idea: use x = cos(q) I =0 f(cos(q))dq =0 F(q)dq
p
trapezoidal rule for 0 F(q)dq has O((Dq)4) error.
iii) Gauss-Laguerre quadrature:
Task: evaluate 0 f(x) e−x dx (1)
* Formula: 𝑘=1 lk 𝑓(xk)
I = σ𝑛
* n=16: (see example next)
* n=4: xk lk
0.322547689619392 0.603154104341633
1.74576110115834 0.357418692437799
4.53662029692112 3.88879085150053E-2
9.39507091230113 5.39294705561327E-4
* n=8:
1.70279632305101E-01 3.69188589341637E-01
9.03701776799380E-01 4.18786780814343E-01
2.25108662986613E+00 1.75794986637171E-01
4.26670017028765E+00 3.33434922612156E-02
7.04590540239346E+00 2.79453623522567E-03
1.07585160101810E+01 9.07650877335821E-05
1.57406786412780E+01 8.48574671627253E-07
2.28631317368892E+01 1.04800117487151E-09
Example:
Example: evaluate I =0 𝑥 𝑛 e−x dx = Gamma function for n=10 (1)
Sum=3628799.99999993
Example:
1 p
Example: evaluate I =0 1+𝑥4 dx (Iexact = 2 2 = 1.11072073453959…) (2)
𝒆𝒙
Solution: First we rewrite the integral as I =0 4 e−xdx
1+𝑥
𝑒𝑥
That is, f(x) =
1+𝑥 4
Next, we use 256-term Gauss-Laguerre quadrature
(see tabulated nodes and weights in a separate file)
Improper Integral
Singular integrand
Unbounded interval
6.5 Improper Integral
▪ If we uses trapezoidal rule or Simpson's rule, f(x=0) is needed in the formulation. However f(0)→∞ .
▪ Note, the singularity, ~O(x-1/2), near x=0 is integrable and the integral I exists.
4
𝑑𝑥
I= න (contd) (Iexact = 1.06394227622575…)
𝑥 𝑒 𝑥 +1
0
▪ If one uses Gaussian quadrature, f(x=0) is NOT needed and the formulae can be directly applied.
▪ However, using composite 4-term Gaussian quadrature, we obtain
I(n=1000) = 1.057818, I(n=2000) = 1.05961335 (8000 function evaluations)
where n is the number of sub-intervals in [0, 4].
▪ Obviously, more terms are still needed to obtain reasonable convergence. And this is not a good approach.
g(x q)
We note f(x) dx ~ qx q-1 dx ~ O(xq-pq-1)dx
x qp
∞ 1
Example: evaluate I=
0 1+𝑥 4
𝑑𝑥 , f(x) = 1/(1+x4) (Iexact = = 1.11072073453959…)
2 2
Solution: Have worked on it using Gauss-Laguerre quadrature. Now use change of variable:
∞
𝑒 𝑡 𝑑𝑡 𝑒𝑡
* Let x = 𝑒𝑡 -1 dx = et dt I = න 𝑡 − 1)4 + 1 , F(t) =
0 (𝑒 (𝑒 𝑡 −1)4 +1
5
* Use Simpson’s rule for න 𝐹 𝑡 𝑑𝑡 (use t =5 to replace t→ ) with h = dt = 0.01
0
5
I ~ 0 𝐹 𝑡 𝑑𝑡 = 1.11072063042711, Error = Iexact - 1.11072063042711 = 1.04112E-7
* Advantage of transformation: bring “” or x= e5 -1=147.413 to t =5. Note: f(147.413) ~2.118E-9 is “sufficiently” small.
* Anatomy of the Error (1.04112E-7):
∞
1
i) It comes mainly from න 𝐹 𝑡 𝑑𝑡 = න 4 𝑑𝑥
5 147.413 1 + 𝑥
∞
1 1 1 1 1
~ = (1- 𝑥 −4 + 𝑥 −8+…) න 𝑑𝑥 ~
∞ 1
(1− 𝑥 −4 + 𝑥 −8 +…)𝑑𝑥= ( 1 − 1
)
1+𝑥 4 𝑥 1+𝑥 −4
4 𝑥4 4 147.413 𝑥 4 3𝑥 3 7𝑥 7 147.413
147.413 1 + 𝑥
= 1.0405668E-7
∞ 1
I=
0 1+𝑥 4
𝑑𝑥 (contd)
∞ 1
Evaluate I= 𝑑𝑥 efficiently and accurately.
0 2
𝑥(1+𝑥 )
1.E+03
( Iexact= 𝜋/ 2 = 2.22144146907918 ) 1.E+02
f
1.E+01
1
* Diagnosis: The challenges for f(x) = 𝑥(1+𝑥 2 )
are 1.E+00
1.E-01
i) it has a strong singularity at x=0 with f(0) →. 1.E-02 75 Algebaric
dacay
1.E-03 50
ii) it decays algebraically (i.e. slowly) as f(x) ~ 1/x2.5 as x→ 1.E-04 singularity
1.E-05 25
so that if the integration stop at x=1000, the error beyond 1.E-06 0
x can easily be on the order of O(1000-1.5) for I, which is large. 1.E-07 0.0 0.1 0.2 0.3 0.4 0.5 x
1.E-08 x
iii) finding a change of variable to suit both 0 & is not easy. 1.E-04 1.E-03 1.E-02 1.E-01 1.E+00 1.E+01 1.E+02 1.E+03
Solution:
* Strategy: divide and conquer.
∞ 1 1 ∞
* Split the interval into (0,1] and [1, ): I = 0 𝑑𝑥 = 0 (… )𝑑𝑥 + 1 (… )𝑑𝑥 I1 + I2
𝑥(1+𝑥 2 )
∞ 1 1 ∞
Divide & conquer strategy for I = 𝑑𝑥 = (… )𝑑𝑥 + (… )𝑑𝑥
0 𝑥(1+𝑥 2 ) 0 1
1 1
* For I1 =0 𝑥(1+𝑥 2 )
𝑑𝑥, use change of variable: x = x2 use a “microscope” to enlarge the x=0 region.
1 𝑑x
I1 =20 𝑑𝑥
1+x4
For this smooth integrand, we can then use 16-term Gauss-Legendre quadrature to obtain
I1 = 1.73394597467982; 12-term G-L: I1 = 1.73394597467931
Total # of function evaluations = 16.
∞ 1
* For I2 =1 𝑑𝑥, let x=et use “telescope” to bring “” a lot closer
𝑥(1+𝑥 2 )
∞
𝑒 𝑡Τ2 𝑑𝑡 ∞ −𝑡 𝑒 𝑡Τ2
I2 = න = 0 𝑒 𝑒 𝑡 +𝑒 −𝑡 𝑑𝑡
0 𝑒 2𝑡 + 1
𝑒 𝑡 Τ2
Let f(t) = . It decays exponentially.
𝑒 𝑡 +𝑒 −𝑡
1 𝑑𝑥
• Consider for example: I= 0.2 2
𝑥
30
The integrand f(x) = 1/ x2 rises rapidly near x = 0.2.
25
How do we know if we have obtained desired accuracy? f(x)
20
Typically, compute I1 with h1 = (b-a)/2,
15
then I2 with h2 = (b-a)/22, ...,
10
In-1,
5
In with hn = (b-a)/2n.
Compare In with In-1, if |In-In-1|<e the result is accepted. 0
0 0.2 0.4 0.6 0.8 1 x
* Using Simpson’s rule with n intervals, hk = (b-a)/2k
𝑏−𝑎 4 (4)
I = Sk[a, b] - ℎ f (x), a≤x≤b k n hk Sk |Sk-Sk-1|
180 𝑘
1 2 0.4 4.948148
Sk[a, b] = [fa + 4 f1 + 2f2 + 4f3 + ... +4fn-1 + fn] * hk /3 2 4 0.2 4.187037 0.76111
3 8 0.1 4.024281 0.16276
* If we set the tolerance to be e=0.02, then S= S5 = 4.000154 4 16 0.05 4.002164 0.02212
Extrapolation: I = (24S5 -S44)/(24-1) = 4.00002 5 32 0.025 4.000154 0.00201
Adaptive scheme
Comments:
• Disadvantage: h is same everywhere in this approach.
• Only need small h near the region where f(x) changes rapidly.
• The above is NOT an adaptive procedure.
Adaptive integration scheme seeks to automatically chooses small h only in regions where f(x) changes rapidly.
◊ Adaptive scheme
* Set e=0.02, evaluate S1[0.2, 1] = [f(0.2) + 4f(0.6)+f(1)] *0.4/3 = 4.948145148, h1 = (1-0.2)/2 =0.4
Evaluate S2[0.2, 0.6] & S2[0.6, 1] using h2 = 0.2: S2[0.2, 0.6] =[f(0.2) + 4f(0.4)+f(0.6)]* 0.2/3 = 3.5185182
S5[0.2, 0.25] S5[0.25, 0.3] Extrap: RS[0.3,0.4] = (16S5 –S4 )/15 =0.8333492
RS[0.3,0.4] = 0.8333492
RS[0.2,0.3] = 1.6666856
1 1 tan 10 x
f( − x) = = = = 1 − f ( x)
2 1 + tan 10
( / 2 − x) 1 + cot 10
x 1 + tan 10
x
/2 /2 /2
Hence f − x dx = 1 − f ( x )dx = − f (x )dx
0 2 0 2 0
/2 0 /2 /2
Since
f − x dx = − f (t )dt = f (t )dt = f ( x )dx ,
0 2 /2 0 0
/2 /2 /2
then f − x dx = f ( x )dx = − f (x )dx
0 2 0 2 0
/2
f ( x )dx =
0 4
• Lesson:
Study the analytical
behavior of the integrand
before using computer for
brute force calculation.
Example 2 Numerical integration for complimentary error function erfc(x)
Develop an accurate and efficient numerical integration procedure to evaluate
the complimentary error function for large x.
Solution:
2 x 2
* The error function is: erf ( x) = exp(−t )dt ; erf () = 1
0
* For small x, we can use Taylor series expansion for erf(x) to obtain erfc(x)
by using
erfc(x) = 1- erf(x)
* However, we have learnt that for large |x|, the roundoff error in the Taylor
series expansion will destroy the accuracy of erf(x) and erfc(x).
* For large |x|, it is advantageous to express it as
2 −t 2
erf(x) = 1 − e dt = 1- erfc(x )
x
2 −t 2
so that erfc(x ) = e dt (1)
x
* Introduce change of variable:
t=v+x (2)
2 − x2 − v 2 − 2 xv
so that erfc(x ) = e e dv
0
2 − x2 − v − v 2 − ( 2 x −1)v
= e e e dv (3)
0
* Recall Gauss-Laguerre quadrature (GLQ):
N
I = 0 f(x) exp(-x) dx = k f(x) (4)
k =1
* There are two advantages of dealing with (3) or using (2):
i) The integration limit is [0, ) so that we can use accurate GLQ procedure
ii) For large value of |x|, exp( − x 2 ) has been factored out of the integration
so that the magnitude of the integrand is no longer very small.
For x=3:
Nodes vi (i) Weight i f(i) i * f(i)
5.63664024461788E-03 1.43841583354197E-2 9.72179352085835E-01 1.39839817308284E-02
2.96993538627220E-02 3.26880042670518E-2 8.61242796294662E-01 2.81523082002475E-02
7.29909700414853E-02 4.91865460452045E-2 6.90539203710237E-01 3.39652383393124E-02
1.35522727629362E-01 6.30083332613029E-2 4.98585570745617E-01 3.14150458008168E-02
2.17298150208849E-01 7.35473272042786E-2 3.21837268945633E-01 2.36702709256759E-02
3.18320587462526E-01 8.04813955215006E-2 1.83979501271005E-01 1.48069270096402E-02
4.38593924750508E-01 8.37782059806297E-2 9.20582035598338E-02 7.71247114004250E-03
5.78122716012576E-01 8.36703647157153E-2 3.97621646872032E-02 3.32691482126463E-03
7.36912219811437E-01 8.06048869119271E-2 1.45873477452929E-02 1.17581151535429E-03
9.14968412092162E-01 7.51760246912409E-2 4.46266456347441E-03 3.35485381412478E-04
1.11229799195779E+00 6.80518234921075E-2 1.11522758509835E-03 7.58932707746425E-05
1.32890838503956E+00 5.99042513766679E-2 2.22509798077039E-04 1.33292828777786E-05
1.56480774598414E+00 5.13507526567855E-2 3.45652397697468E-05 1.77495107793875E-06
1.82000496063290E+00 4.29121916845640E-2 4.06765919676904E-06 1.74552171159233E-07
2.09450964813355E+00 3.49890164176762E-2 3.52040747962061E-07 1.23175595101356E-08
2.38833216309314E+00 2.78546588330936E-2 2.17035208864264E-08 6.04544169768328E-10
2.70148359782584E+00 2.16631054438426E-2 9.21119241503868E-10 1.99543032550506E-11
3.03397578472306E+00 1.64664049048934E-2 2.59490014739922E-11 4.27286765148432E-13
3.38582129876156E+00 1.22376124792404E-2 4.66792126804301E-13 5.71242115619148E-15
3.75703346015866E+00 8.89512796696100E-3 5.14645780598107E-15 4.57784007607669E-17
4.14762633718054E+00 6.32530484500813E-3 3.33012773502293E-17 2.10640730968365E-19
4.55761474910781E+00 4.40132094141652E-3 1.20827149292846E-19 5.31799062474263E-22
4.98701426936161E+00 2.99737443582080E-3 2.34313406260787E-22 7.02325013896175E-25
5.43584122879277E+00 1.99814663199109E-3 2.30954680331271E-25 4.61481316646508E-28
Sum=1.58635639863988E-01
I = 0b x dx (b~O(1))
using Simpson’s rule. Investigate how we lose the O(h4) global accuracy.
Solution: Let’s start by considering the first two sub-intervals, x = [0, 2h]:
0.35
0.3 data
0.25 f(x)
0.2 Parabola
0.15
0.1
0.05
0
=0.5
-0.05
-0.05 0 0.05 0.1 S
h 1
Simpson: S[0, 2h] = 3 (0 + 4h+ (2h)) = 3 (4+2) h+
2h 1
Exact: I(0, 2h) = 0 x dx = (2h) +
1+
+
2+ 1
Error: E(0, 2h) = I(0, 2h) - S[0, 2h] = h [ - (4+2)]
1+ 3
Note: a) For =1, 2, & 3, coef_1 = 0 E(0, 2h) = 0
(Simpson’s rule is exact for polynomials of order 1, 2, & 3.)
b) When is not an integer and < 3, coef_1 0 and +1< 4,
E(0, 2h) h+ » h (=order of TE in smooth region)
c) When =0.5, we end up with O(h) error—as clearly shown
2
in the case of I = 0 xdx for =0.5.
2+ 1
Plot of coef_1() - (4+2) vs :
1+ 3
0.35 coef_1
0.3
0.25
0.2
0.15
0.1
0.05
0
-0.05 0 0.5 1 1.5 2 2.5 3
1.E-01
Error_0.1
1
Error_0.5
1.1
1.E-02
1
=0
1.5
1.E-03
=0
1.E-04
10 100 n 1000
1.E+00
coef_n(0.1)
1.E-01
coef_n(0.5)
1.E-02
1.E-03
1.E-04
1.E-05
1.E-06 =0
1.E-07
=0
1.E-08
n
1.E-09
1 10 100
Now, can you perform a similar analysis when the trapezoidal rule is used?
Example 4: Divide & conquer strategy
dx
Evaluate I= efficiently and accurately.
2
0 x ( x + 1)
1.E+03
f
1.E+02
1.E+01
1.E+00
1.E-01
1.E-02 75 Algebaric
dacay
1.E-03 50
1.E-04 singularity
1.E-05 25
1.E-06 0
1.E-07 0.0 0.1 0.2 0.3 0.4 0.5 x
1.E-08 x
1.E-04 1.E-03 1.E-02 1.E-01 1.E+00 1.E+01 1.E+02 1.E+03
1
* Diagnosis: The challenges for f ( x) = are:
x ( x 2 + 1)
i) it has a strong singularity at x=0 with f(0) →.
ii) it decays algebraically (a.k.a. slowly) as f(x) ~ 1/x2.5 as x→ so that if
the integration stop at x=1000, the error beyond x can easily be on
the order of O(1000-1.5) for I, which is still large.
iii) finding a change of variable that can suit both 0 and infinity is not
easy.
(Note: Iexact= / 2 = 2.22144146907918 is known in this case.
The technique we will discuss is useful for handling more complicated
integrands for which exact values are unknown).
Solution:
* Strategy: divide and conquer.
* Split the interval into (0,1] and [1, ):
dx 1 dx dx
I= 2
= 2
+ 2
I1 + I2
0 x ( x + 1) 0 x ( x + 1) 1 x ( x + 1)
1 dx
* For I1 =
0 x ( x 2 + 1)
2
we use change of variable: x =
so that “microscope” is used to enlarge the x=0 region.
1 d
I1 = 2
0 4 +1
For this smooth integrand, we can then use 12 term Gauss-Legendre
quadrature to obtain
I1 = 1.73394597467934;
Total # of function evaluations = 12.
dx
* For I2 = ,
1 x ( x 2 + 1)
Let x=et
so that we use “telescope” to bring “” a lot closer.
et / 2 dt et / 2
−t
I2 = 2t = e t −t
dt
0 (e + 1) 0 (e + e )
et / 2
Let g(t) = t − t . It decays exponentially.
(e + e )
Because t goes from 0 to , it is ideal to use Gauss-Laguerre
quadrature.
Here we use 128 nodes Gauss-Laguerre quadrature:
Nodes ti Weight wi g(ti) wi*g(ti)
1.12513882636759E-02 2.85518444532397E-02 5.0278894873E-01 1.4355551857E-02
5.92847412690264E-02 6.33502117845051E-02 5.1413925724E-01 3.2570830833E-02
1.45707966594312E-01 9.13083813661343E-02 5.3212798139E-01 4.8587744660E-02
2.70553178758665E-01 1.09913900410911E-01 5.5209667018E-01 6.0683098423E-02
4.33841407553836E-01 1.18274171034173E-01 5.6692759904E-01 6.7052891813E-02
6.35597665781622E-01 1.17045739000406E-01 5.6833375186E-01 6.6521043985E-02
8.75852384546521E-01 1.08089987545568E-01 5.4996637465E-01 5.9445858586E-02
1.15464170197439E+00 9.39428886389285E-02 5.1067395561E-01 4.7974186542E-02
1.47200756316673E+00 7.72536687978980E-02 4.5506351659E-01 3.5155326192E-02
1.82799777831235E+00 6.03270562656615E-02 3.9082063872E-01 2.3577058662E-02
2.22266607156190E+00 4.48473482471952E-02 3.2530309095E-01 1.4588981006E-02
2.65607212988348E+00 3.17969479368768E-02 2.6369680889E-01 8.3847537033E-03
3.12828165502791E+00 2.15301494537944E-02 2.0886716227E-01 4.4969412198E-03
3.63936641985240E+00 1.39369517338463E-02 1.6196532152E-01 2.2573028685E-03
4.18940432959404E+00 8.63158538020225E-03 1.2307863464E-01 1.0623637434E-03
4.77847948843487E+00 5.11777701366923E-03 9.1692888987E-02 4.6926375958E-04
5.40668227160050E+00 2.90634743648595E-03 6.6979996472E-02 1.9466714104E-04
6.07410940319653E+00 1.58143294331667E-03 4.7975731034E-02 7.5870401537E-05
6.78086403997562E+00 8.24738985098812E-04 3.3694073795E-02 2.7788816225E-05
7.52705586122937E+00 4.12326088539694E-04 2.3201734959E-02 9.5666806228E-06
8.31280116500777E+00 1.97649426442591E-04 1.5663836320E-02 3.0959482645E-06
9.13822297088039E+00 9.08515788782451E-05 1.0367166886E-02 9.4187348006E-07
1.00034511294682E+01 4.00484927835805E-05 6.7263302472E-03 2.6937938836E-07
1.09086224389908E+01 1.69307623980817E-05 4.2778222463E-03 7.2426792034E-08
1.18538807690918E+01 6.86452529111068E-06 2.6666283773E-03 1.8305137938E-08
1.28393771922232E+01 2.66921659814210E-06 1.6291634836E-03 4.3485902116E-09
1.38652701228920E+01 9.95364010286385E-07 9.7542716843E-04 9.7090509811E-10
1.49317254650919E+01 3.55943575300306E-07 5.7229112713E-04 2.0370334990E-10
1.60389167682670E+01 1.22053255194881E-07 3.2899816519E-04 4.0155297015E-11
1.71870253921812E+01 4.01279192093563E-08 1.8530402635E-04 7.4358649985E-12
1.83762406810896E+01 1.26481141474759E-08 1.0224687350E-04 1.2932301272E-12
1.96067601476416E+01 3.82148972942657E-09 5.5264485340E-05 2.1119266313E-13
2.08787896669713E+01 1.10664105922734E-09 2.9256908227E-05 3.2376895910E-14
2.21925436814678E+01 3.07100923709742E-10 1.5168770125E-05 4.6583433169E-15
2.35482454167489E+01 8.16549938415449E-11 7.7012900177E-06 6.2884878896E-16
But we only need to compute up to k=36 as w36g(t36) = 6.288E-16 is
sufficiently small.
Add up those 36 terms of wi*g(ti) in the last column, we obtain:
I2 = 0.48749549439936 (with 36 f(x) evaluations for I2)
1 e− x
* Near x=, the integrand f(x) = is dominated by 1/ x 2 .
x 2 ( x 2 + 1)
e− x
* Consider the non-singular part of the integrand, g(x) = .
( x 2 + 1)
Its Taylor series expansion near x=0 is
g(x) ~ (1- x + x2/2! + …) (1- x2 +…) = 1 - x - x2/2+ …
Thus
g(x) / x2 ~ 1/ x2 – 1/x - 1/2 + … near x=0.
In the above, the first two terms are singular but can be handled
analytically while “-1/2” is not singular any more.
* Thus we rewrite g(x) / x2 as
2 1− x 1 − x − e − x /(1 + x 2 )
f(x) = g(x) / x = -
x2 x2
20 1 − x 20 1 − x − e − x /(1 + x 2 )
Hence, I() = dx - dx
2 2
x x
= 1/ - 1/20 – ln(20/) – J()
where
20 1 − x − e − x /(1 + x 2 )
J() = dx
2
x
Note, the integrand for J() is well behaved at x= and numerical integration
is very easy to carry out. Here, Romberg integration based on trapezoidal
rule (with a total of n sub-intervals) is used for = 0.0001 to obtain:
(1) ( 2) (3)
n J(= 0.0001) Jn Jn Jn
50 -1.990208809379
100 -1.998751251663 -2.001598732
200 -2.000851202826 -2.001551187 -2.001548016821
400 -2.001374165688 -2.001548487 -2.001548306648 -2.001548311248
On the other hand, the direct application of trapezoidal rule using 5000
…