romberg integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf ·...

23
Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia function 2 The Euler-Maclaurin Summation Formula justifying the use of extrapolation a Julia function MCS 471 Lecture 14(a) Numerical Analysis Jan Verschelde, 20 July 2018 Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 1 / 23

Upload: others

Post on 17-Jul-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

Romberg Integration

1 Adaptive Integrationstopping the composite trapezium rulea Julia function

2 The Euler-Maclaurin Summation Formulajustifying the use of extrapolationa Julia function

MCS 471 Lecture 14(a)Numerical Analysis

Jan Verschelde, 20 July 2018

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 1 / 23

Page 2: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

Romberg Integration

1 Adaptive Integrationstopping the composite trapezium rulea Julia function

2 The Euler-Maclaurin Summation Formulajustifying the use of extrapolationa Julia function

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 2 / 23

Page 3: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

stopping the composite trapezium rule

The composite trapezium ruleapplied to f (x) over [a,b] with n subintervals is∫ b

af (x)dx ≈ h

2

(f (a) + f (b)

)+ h

n−1∑i=1

f (a + ih), h =b − a

n.

The composite trapezium rule is straighforward to apply,but in general we do not know a good apriori value for n.

Our problem is to apply the composite trapezium rule in an iterativealgorithm: we stop when the accuracy of the approximation is good.

We can estimate the accuracy by comparingthe last two approximations.

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 3 / 23

Page 4: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

an iterative composite trapezium rule

Input: f (x) and [a,b] define∫ b

af (x)dx ,

N is an upper bound on the number of subintervals,ε is the accuracy requirement.

Output: an approximation for∫ b

af (x)dx

n is the number of subintervals computed.

T1 :=b − a

2

(f (a) + f (b)

)for n from 2 to N do

h := (b − a)/n

Tn :=h2

(f (a) + f (b)

)+ h

n−1∑i=1

f (a + ih)

if |Tn − Tn−1| < ε then return (Tn,n)return (TN ,N)

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 4 / 23

Page 5: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

the new function evaluations

a bt ttt tt t t tt t t t t t t tt t t t t t t t t t t t t t t tThe number of new function evaluations is 1,2,4,8,16.

Observe the distance between the new function evaluations.

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 5 / 23

Page 6: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

the formulas

Tn =b − a

2n

(f (a) + f (b)

)+

b − an

n−1∑i=1

f(

a + ib − a

n

)

T2n =b − a

4n

(f (a) + f (b)

)+

b − a2n

2n−1∑i=1

f(

a + ib − a

2n

)We can separate the new function evaluations for the previous sum:

T2n =12

Tn +b − a

2n

n−1∑i=1

f(

a +b − a

2n+ i

b − an

)

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 6 / 23

Page 7: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

on the composite midpoint rule

Exercise 1:

Consider the midpoint rule:∫ b

af (x)dx = (b − a)f

(a + b

2

).

1 Give the formula to apply the composite midpoint rule on nsubintervals of [a,b].

2 In order to recycle all function evaluations in the next step in aniterative application of the composite midpoint rule, justify whyeach subinterval should be divided in three.

3 Write a Julia function to apply the composite midpoint rule to∫ π/2

0cos(x)dx with 10−4 as the tolerance for the stopping

criterium. Compare the results with the adaptive compositetrapezium rule.

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 7 / 23

Page 8: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

Romberg Integration

1 Adaptive Integrationstopping the composite trapezium rulea Julia function

2 The Euler-Maclaurin Summation Formulajustifying the use of extrapolationa Julia function

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 8 / 23

Page 9: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

specifications of a Julia function

function adaptrap(f::Function,a::Float64,b::Float64,n::Int64,tol::Float64)

## Returns a vector of at most n approximations# for the definite integral of the function f# over the interval [a,b], the i-th entry in the# returned vector uses 2^i function evaluations.# Stops when the difference between two# consecutive approximations is less than tol.## Example:# t = adaptrap(cos,0,pi/2,10,1.e-5)#

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 9 / 23

Page 10: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

definition of the functiont = zeros(n)h = (b-a) # size of subintervalm = 1 # number of subintervalst[1] = (f(a) + f(b))*h/2for i = 2:n

h = h/2for j=0:m-1

t[i] = t[i] + f(a+h+j*2*h)end;t[i] = t[i-1]/2 + h*t[i]if(abs(t[i] - t[i-1])) < tol

return t[1:i], iendm = 2*m

endreturn t, n

endNumerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 10 / 23

Page 11: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

the main test functionfunction main()## Applies adaptive integration.#

exact = 1.0n = 20tol = 1.0e-4t, nit = adaptrap(cos,0.0,pi/2,n,tol)println("The composite trapezium rule :")for i=1:length(t)

strerr = @sprintf("%.2e", abs(exact - t[i]))strapp = @sprintf("%.16e", t[i])println("$strapp $strerr")

endprint("The number of iterations : ")println(nit)

endNumerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 11 / 23

Page 12: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

running the script

The stop tolerance is 1.0e-4.

The composite trapezium rule :7.8539816339744828e-01 2.15e-019.4805944896851990e-01 5.19e-029.8711580097277540e-01 1.29e-029.9678517188616966e-01 3.21e-039.9919668048507226e-01 8.03e-049.9979919432001874e-01 2.01e-049.9994980009210144e-01 5.02e-059.9998745011752632e-01 1.25e-05The number of iterations : 8

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 12 / 23

Page 13: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

Romberg Integration

1 Adaptive Integrationstopping the composite trapezium rulea Julia function

2 The Euler-Maclaurin Summation Formulajustifying the use of extrapolationa Julia function

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 13 / 23

Page 14: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

motivationWe observe a very slow convergence for∫ π/2

0cos(x)dx

with the composite trapezium rule.

2 7.8539816339744828e-01 2.15e-014 9.4805944896851990e-01 5.19e-028 9.8711580097277540e-01 1.29e-02

16 9.9678517188616966e-01 3.21e-0332 9.9919668048507226e-01 8.03e-0464 9.9979919432001874e-01 2.01e-04

128 9.9994980009210144e-01 5.02e-05256 9.9998745011752632e-01 1.25e-05512 9.9999686253528774e-01 3.14e-061024 9.9999921563419114e-01 7.84e-072048 9.9999980390857179e-01 1.96e-074096 9.9999995097714434e-01 4.90e-088192 9.9999998774428667e-01 1.23e-08

16384 9.9999999693607278e-01 3.06e-0932768 9.9999999923401672e-01 7.66e-1065536 9.9999999980850662e-01 1.91e-10131072 9.9999999995212607e-01 4.79e-11262144 9.9999999998802369e-01 1.20e-11524288 9.9999999999699174e-01 3.01e-12

1048576 9.9999999999924849e-01 7.52e-13

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 14 / 23

Page 15: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

justification the extrapolation

The error expansion of the composite trapezoidal rule hasonly even powers of h.We can see this by the Euler-Maclaurin summation formula.

TheoremFor g ∈ C2m+2[0,N] (g is sufficiently many times continuouslydifferentiable over [0,N]):

12

g(0) + g(1) + · · ·+ g(N − 1) +12

g(N) =

∫ N

0g(t)dt

+m∑

l=1

B2l

(2l)!

(g(2l−1)(N)− g(2l−1)(0)

)+

B2m+2

(2m + 2)!Ng(2m+2)(α),

where α ∈ [0,N] and where Bk are the Bernoulli numbers.

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 15 / 23

Page 16: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

the connection with the trapezium rule

To see the connection with the composite trapezoidal rule,we make a change of coordinates:

[0,N]→ [a,b] : t 7→ x = a + ht , h =b − a

N, dx = hdt ,

so ∫ N

0g(t)dt =

∫ b

af (x)

1h

dx .

To replace the derivatives of g(t) in the equation of the theorem,we observe g(t) = f (a + ht) = f (x) and apply the chain rule:

g′(t) = f ′(x)h and for any ` : g(`)(t) = f (`)(x)h`.

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 16 / 23

Page 17: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

rewriting the formula

After executing the coordinate change,the formula of the theorem turns into

12

f (a) + f (a + h) + · · ·+ f (b − h) +12

f (b) =∫ b

af (x)

1h

dx

+m∑

l=1

B2l

(2l)!h2l−1

(f (2l−1)(b)− f (2l−1)(a)

)+

B2m+2

(2m + 2)!Nh2m+2f (2m+2)(β), β ∈ [a,b].

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 17 / 23

Page 18: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

the error formula

Multiplying by h, we obtain the error formula for the compositetrapezoidal rule T (h):

T (h) =

∫ b

af (x)dx

+m∑

l=1

B2l

(2l)!h2l(

f (2l−1)(b)− f (2l−1)(a))

+B2m+2

(2m + 2)!(Nh)h2m+2f (2m+2)(β).

Observe that only even powers of h occur.This justifies the extrapolation formula used in Romberg integration.

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 18 / 23

Page 19: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

deriving the extrapolation formula

T (h) =

∫ b

af (x)dx + C1h2 + · · ·

T (h/2) =

∫ b

af (x)dx + C1

h2

4+ · · ·

We eliminate C1 by

22T (h/2)− T (h)22 − 1

=

∫ b

af (x)dx + O(h4).

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 19 / 23

Page 20: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

Romberg Integration

1 Adaptive Integrationstopping the composite trapezium rulea Julia function

2 The Euler-Maclaurin Summation Formulajustifying the use of extrapolationa Julia function

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 20 / 23

Page 21: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

a Julia functionfunction romberg(t::Array{Float64,1})## Applies extrapolation to the approximations in t,# t is a sequence of results of the composite Trapezoidal rule.## Example:# t = adaptrap(cos,0,pi/2,6)# et = romberg(t);#

n = length(t)et = zeros(n,n)et[:,1] = tfor i = 2:n

m = 2for j = 2:i

r = 2^met[i,j] = (r*et[i,j-1] - et[i-1,j-1])/(r - 1);m = m+2

endendreturn et

end

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 21 / 23

Page 22: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

running the script

The composite trapezium rule :2 7.8539816339744828e-01 2.15e-014 9.4805944896851990e-01 5.19e-028 9.8711580097277540e-01 1.29e-02

16 9.9678517188616966e-01 3.21e-0332 9.9919668048507226e-01 8.03e-0464 9.9979919432001874e-01 2.01e-04

Romberg integration :7.8539816339744828e-01 2.15e-011.0022798774922104e+00 2.28e-039.9999156547299273e-01 8.43e-061.0000000081440208e+00 8.14e-099.9999999999801692e-01 1.98e-121.0000000000000002e+00 2.22e-16

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 22 / 23

Page 23: Romberg Integration - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs471/romberg.pdf · Romberg Integration 1 Adaptive Integration stopping the composite trapezium rule a Julia

the progress of the extrapolation errorsRomberg integration :7.8539816339744828e-01 2.15e-011.0022798774922104e+00 2.28e-039.9999156547299273e-01 8.43e-061.0000000081440208e+00 8.14e-099.9999999999801692e-01 1.98e-121.0000000000000002e+00 2.22e-16

Errors in the table2.15e-015.19e-02 2.28e-031.29e-02 1.35e-04 8.43e-063.21e-03 8.30e-06 1.24e-07 8.14e-098.03e-04 5.17e-07 1.90e-09 2.98e-11 1.98e-122.01e-04 3.23e-08 2.96e-11 1.15e-13 1.78e-15 2.22e-16

Numerical Analysis (MCS 471) Romberg Integration L-14(a) 20 July 2018 23 / 23