Anda di halaman 1dari 9

Numerical integration example

Consider the function f(x) shown below. The purpose of the exercise is to integrate the function
between the limits a and b.
1
f( x ) := a := 1 b := 3
x
b

A symbolic integration is shown next: ⎮ f( x ) dx → ln( 3 )

a

b
Using the equal sign, Mathcad calculates ⌠
⎮ f( x ) dx = 1.099
its own numerical approximation: ⌡
a

A numerical integration using rectangles is calculated next, with n := 10

b−a
The interval a<x<b is divided into n equally-spaced intervals of size: Δx :=
n

Using rectangles with the height of the rectangle given by the left-hand side of each
subinterval results in the following approximation for the integral. The graph illustrates
the area being calculated. The index j runs from zero to n. The arrays x and y are
used for both calculating the integrals as well as for plotting . The array t is used for
the graphs only.

j := 0 .. n x := a + j⋅ Δx
j
y := f x
j ( j) t := a , a + 0.01 .. b

0.8
f( t)

yj
0.6

yj

0.4

0.2
1 1.5 2 2.5 3
t, x j , x j

n− 1

The approximation for the integral is:


IL :=
∑ ⎡⎣(f(xk))⋅Δx⎤⎦ IL = 1.168
k=0

Numerical integration - page 1


The following figure shows the integral calculation using rectangles when the rectangle height is
given by the right-hand side ordinate of the sub-intervals.
k := 0 .. n + 1 x := a + k ⋅ Δx
k
y := f x
k ( k)

0.8
f( t)

yk+ 1
0.6

yk+ 1

0.4

0.2
1 1.5 2 2.5 3
t , xk , xk

n− 1

∑ ⎡⎣(f(xk+1))⋅Δx⎤⎦
The approximation for the integral is:
IR := IR = 1.035
k= 0

The figure below illustrates the approximation of the integral using the trapezoidal rule.

0.8
f( t)
yj
0.6
yj

0.4

0.2
1 1.5 2 2.5 3
t, x j , x j

n− 1
⎡1 ⋅ f x + f x ⎤
IT :=
∑ ⎢ ( ( k) ( k+1) ) ⋅ Δx⎥ IT = 1.102
The approximation for the integral is: ⎣2 ⎦
k=0

Numerical integration - page 2


The following approximation uses the Simpson rule:

⎛ n n
−1 ⎞
⎜ 2 2 ⎟

IS := f( x ) + 4 ⋅ f( x f( x ) + f( x )⎟ ⋅
Δx
⎜ 0 ∑ 2 ⋅ k − 1) + 2⋅
∑ 2 ⋅ k n ⎟ 3
IS = 1.099
⎝ k= 1 k=1 ⎠

Convergence of the different methods


To analyze the variation of the different approximations to the integral with the number of
intervals n in the range a<x<b, we put together the following functions for IL, IR, IT, and IS as
functions of n. To put together these functions we borrow the expressions for IL, IR, IT, and IS
b−a
as shown above, but making the replacement x = a + k ⋅ , thus:
k n

Rectangular approximation, left-hand side:

n− 1
⎡f⎛ a + k⋅ b − a ⎞ ⋅ ⎛ b − a ⎞⎤
IL( n ) :=
∑ ⎢⎜
⎣⎝
⎟⎜
n ⎠ ⎝ n ⎠⎦
⎟⎥
k= 0

Rectangular approximation, right-hand side:

n− 1
⎡f⎡a + ( k + 1 ) b − a⎤ ⋅ ⎛ b − a ⎞⎤
IR( n ) :=
∑ ⎢⎢
⎣⎣
⎥⎜
n ⎦ ⎝ n ⎠⎦
⎟⎥
k=0

Trapezoidal approximation:

n− 1
⎡ 1 ⋅ ⎡f⎛ a + k⋅ b − a ⎞ + f⎡a + ( k + 1) b − a⎤⎤ ⋅ ⎛ b − a ⎞⎤
IT( n ) :=
∑ ⎢ ⎢⎜
⎣2 ⎣ ⎝ n ⎠
⎟ ⎢

⎥⎥ ⎜
n ⎦⎦ ⎝ n ⎠⎦
⎟⎥
k= 0

Simpson's rule approximation:

⎡ n n
−1

⎢ 2 2 ⎥
⎢ ⎡ ⎡ b − a⎤⎤ ⎛ b − a⎞ ⎥ b−a
⎢ ∑
IS ( n ) := f( a) + 4 ⋅ f⎢a + ⎢( 2 ⋅ k − 1 ) ⋅
⎣ ⎣
⎥⎥ + 2⋅
n ⎦⎦ ∑f⎜ a + 2k ⋅
⎝ n ⎠
⎟ + f( b )⎥ ⋅
3⋅ n
⎣ k= 1 k=1 ⎦

Numerical integration - page 3


To verify the functions IL, IR, IT, and IS, defined above, we evaluate them at the value n = 10, and
compare with the results obtained earlier:

IL( 10) = 1.168 IR( 10) = 1.035 IT( 10) = 1.102 IS ( 10) = 1.099

Next, we produce an array of values of n, making sure it includes only even numbers to satisfy
the requirement of Simpson's rule. Subsequently, we produce array of values of IL, IR, IT, and
IS to verify how the integral converges to a value as n increases:

n := 2 , 4 .. 10

ILL := IL( n ) IRR := IR( n ) ITT := IT( n ) ISS := IS ( n )


n n n n
2 2 2 2
n =
2 ⎛ 0 ⎞ ⎛ 0 ⎞ ⎛ 0 ⎞ ⎛ 0 ⎞
4 ⎜ 1.5 ⎟ ⎜ 0.833 ⎟ ⎜ 1.167 ⎟ ⎜ 2 ⎟
6 ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
8 ILL = ⎜ 1.283 ⎟
IRR = ⎜ 0.95 ⎟
ITT = ⎜ 1.117 ⎟
ISS = ⎜ 1.1 ⎟

10 ⎜ 1.218 ⎟ ⎜ 0.996 ⎟ ⎜ 1.107 ⎟ ⎜ 1.099 ⎟


⎜ 1.187 ⎟ ⎜ 1.02 ⎟ ⎜ 1.103 ⎟ ⎜ 1.099 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎝ 1.168 ⎠ ⎝ 1.035 ⎠ ⎝ 1.102 ⎠ ⎝ 1.099 ⎠
Notice that the Simpson rule converges to the value of the integral with n = 6 sub-intervals, while
the other three methods have not converged with n = 10 sub-intervals.

n := 2 , 4 .. 24

ILL := IL( n ) IRR := IR( n ) ITT := IT( n ) ISS := IS ( n )


n n n n
2 2 2 2

n = 0 0 0 0
0 0 0 0 0 0 0 0
2
1 1.5 1 0.833 1 1.167 1 2
4
2 1.283 2 0.95 2 1.117 2 1.1
6
3 1.218 3 0.996 3 1.107 3 1.099
8
4 1.187 4 1.02 4 1.103 4 1.099
10
12 ILL = 5 1.168
IRR = 5 1.035
ITT = 5 1.102
ISS = 5 1.099
6 1.156 6 1.045 6 1.101 6 1.099
14
7 1.148 7 1.053 7 1.1 7 1.099
16
8 1.141 8 1.058 8 1.1 8 1.099
18
9 1.137 9 1.062 9 1.1 9 1.099
20
10 1.133 10 1.066 10 1.099 10 1.099
22
11 1.13 11 1.069 11 1.099 11 1.099
24
12 1.127 12 1.071 12 1.099 12 1.099
Extending the range of n to 24 shows the trapezoidal rule converging to the value of the
integral for n = 20 sub-intervals.

Numerical integration - page 4


The following graph shows the variation of the different integral approximations with n in the
interval 0 < n < 24:

ILL n
2

IRR n 1.5
2

ITT n
2
1
ISS n
2

0.5
0 5 10 15 20
n, n, n, n

The following results show values of IL and IR as n increases up to a value n = 6000:

IL( 50) = 1.112 IR( 50) = 1.085 IL( 800 ) = 1.099 IR( 800 ) = 1.098
IL( 100 ) = 1.105 IR( 100 ) = 1.092 IL( 1600) = 1.099 IR( 1600) = 1.098
IL( 200 ) = 1.102 IR( 200 ) = 1.095 IL( 3600) = 1.099 IR( 3600) = 1.098
IL( 400 ) = 1.1 IR( 400 ) = 1.097 IL( 6000) = 1.099 IR( 6000) = 1.099

Notice that the left-hand side rectangular approach (IL) converges by n = 800, while the
right-hand side rectangular approach (IR) converges by n = 6000.

Integration of data from a table


When the data is given as a table of values (x,y), an integral of the values of y using the
trapezoidal rule as illustrated below. The data is given in the rows of matrix XY, with x being
the first row, and y the second. Vectors of data are extracted form matrix XY, and the value of
n obtained from the length of the vector.

⎛ 2.3 3.6 5.2 6.1 8.2 11.3 14.3 15.2 18.4 19.3 ⎞
XY := ⎜ ⎟
⎝ 12.5 17.2 11.5 9.2 6.3 4.8 9.2 10.5 11.3 12.8 ⎠

〈〉
x := XY( T )0 (
y := XY
T
〈〉
)1

Numerical integration - page 5


The following graph shows the plot of y-vs-x:

20

15
y
10
y
5

0
2 4 6 8 10 12 14 16 18 20
x, x

The integral of y = f(x) for this discrete data is calculated as follows:

n := length ( x ) − 1 n=9

n− 1
⎡1 ⋅ y + y ⎤
ITD :=
∑ ⎢ ( k k+1) ⋅ ( x k+1 − x k)⎥ ITD = 160.65
⎣ 2 ⎦
k=0

An example from probability calculations:


Integrals are used to calculate probabilities of intervals of continuous random variables. If X is a
continuous random variable with a probability density function, or pdf, f(x), the probability that a
realization of the random variable will be contained in the interval a<X<b is given by

b

P( a < X < b ) = ⎮ f( x ) dx

a

Suppose that the random variable X follows the normal distribution with mean μ and standard
deviation σ, then, the pdf is given by

⎡ −( x − μ ) 2⎤
⋅ exp⎢ ⎥,
1
f( x ) =
σ ⋅ 2⋅ π ⎢ 2 ⎥
⎣ 2⋅ σ ⎦
for −∞ < x < ∞. This function has no closed-form integral, therefore, it's well suited for a
numerical integration.

As an example, we'll use the values:

3 3
μ := 1500 σ := 300 a := μ − σ a = 1.2 × 10 b := μ + σ b = 1.8 × 10

Numerical integration - page 6


The pdf is defined next:

⎡ −( x − μ ) 2⎤
⋅ exp⎢ ⎥
1
f( x ) :=
σ ⋅ 2⋅ π ⎢ 2 ⎥
⎣ 2⋅ σ ⎦

b

A plot of the pdf and the area represented by the integral P( a < X < b ) = ⎮ f( x ) dx is shown

a
below:

x := μ − 4 ⋅ σ , μ − 4 ⋅ σ + 10 .. μ + 4 ⋅ σ xs := ( a b ) ys := ( f( a) f( b ) )

0.002

f( x)
0.001
ys

0
0 1000 2000 3000
x , xs

Next, we borrow the definitions of functions IL, IR, IT, and IS:

n− 1
⎡f⎛ a + k⋅ b − a⎞ ⎛b − a ⎞⎤
IL( n ) :=
∑ ⎢⎜
⎣⎝ n
⎟ ⋅⎜
⎠⎝ n
⎟⎥
⎠⎦
k= 0

n− 1
⎡f⎡a + ( k + 1 ) b − a⎤ ⋅ ⎛ b − a ⎞⎤
IR( n ) :=
∑ ⎢⎢
⎣⎣
⎥⎜
n ⎦ ⎝ n ⎠⎦
⎟⎥
k=0

n− 1
⎡ 1 ⋅ ⎡f⎛ a + k⋅ b − a ⎞ + f⎡a + ( k + 1) b − a⎤⎤ ⋅ ⎛ b − a ⎞⎤
IT( n ) :=
∑ ⎢ ⎢⎜
⎣2 ⎣ ⎝ n ⎠
⎟ ⎢

⎥⎥ ⎜
n ⎦⎦ ⎝ n ⎠⎦
⎟⎥
k= 0

⎡ n n
−1

⎢ 2 2 ⎥
IS ( n ) := ⎢f( a) + 4 ⋅ ⎡ ⎡ b − a⎤⎤ ⎛ b − a⎞ ⎥ b−a
⎢ ∑ f⎢a + ⎢( 2 ⋅ k − 1 ) ⋅
⎣ ⎣
⎥⎥ + 2⋅
n ⎦⎦ ⎝ ∑
f⎜ a + 2k ⋅
n ⎠
⎟ + f( b )⎥ ⋅
3⋅ n
⎣ k= 1 k=1 ⎦

Numerical integration - page 7


NOTE: We need to copy and paste the definitions of the functions IL, IR, IT, and IS once again,
or these functions would use the earlier definition of f(x) = 1/x, rather than the pdf function
defined for this exercise.
The following results show the numerical integration for the number of sub-intervals, n, varying
from 2 to 24 (even numbers only to satisfy the requirements of the Simpson's rule):
n := 2 , 4 .. 24

ILL := IL( n ) IRR := IR( n ) ITT := IT( n ) ISS := IS ( n )


n n n n
2 2 2 2

n = 0 0 0 0
0 0 0 0 0 0 0 0
2
1 0.641 1 0.641 1 0.641 1 1.016
4
2 0.673 2 0.673 2 0.673 2 0.683
6
3 0.678 3 0.678 3 0.678 3 0.683
8
4 0.68 4 0.68 4 0.68 4 0.683
10
5 0.681 5 0.681 5 0.681 5 0.683
12 ILL = IRR = ITT = ISS =
6 0.682 6 0.682 6 0.682 6 0.683
14
7 0.682 7 0.682 7 0.682 7 0.683
16
8 0.682 8 0.682 8 0.682 8 0.683
18
9 0.682 9 0.682 9 0.682 9 0.683
20
10 0.682 10 0.682 10 0.682 10 0.683
22
11 0.682 11 0.682 11 0.682 11 0.683
24
12 0.682 12 0.682 12 0.682 12 0.683

The following graph shows the variation of the different integral approximations with n in the
interval 0 < n < 24:

ILL n
2
0.685
IRR n
2

ITT n 0.68
2

ISS n
0.675
2

0.67
0 5 10 15 20
n, n, n, n

Numerical integration - page 8


While the purpose of this exercise is to show the behavior of the numerical integration
approximations, using Mathcad one needs just evaluate the integral but using the definite integral
symbol (Shift 7), replacing the values of the limits and the integrand, and typing an equal sign,
i.e.:

b

⎮ f( x ) dx = 0.683

a

Although the normal distribution pdf does not have a closed-form expression, it can be evaluated
symbolically by using Cntl . (control period) -- instead of the equal sign -- in the expression
above, i.e.,

⎛ 1⎞

b ⎜ 1 2⎟
⎮ f( x ) dx → erf ⎜ ⋅ 2 ⎟
⌡ ⎝2 ⎠
a

The function erf shown in the right-hand side of this result is referred to as the error function.
To see the definition of the error function, select the option Help>Mathcad Help, and click on
the Index button. Type erf in the search field and select the entry erf function in the list of
references shown. This will open the Mathcad Help facility entry for the erf function.

A plot of the error function is shown below:


x := −4 , −3.9 .. 4

erf ( x) 0

1
5 0 5

To see Mathcad's version of the integral of xthe normal distribution pdf, we write it using mean
value η and standard deviation ω, instead of μ and σ (since those variables have been already
defined with numeric values):

⎛⎜ 1
1⎞⎟

⎜ 1 2
2
1 η 2 ⎟
⎮ ⎡ 2⎤ erf ⎜ ⋅ ⋅ξ − ⋅ ⋅2 ⎟
−( ξ − η ) ⎥ ⎝2 ω 2 ω ⎠
⋅ exp⎢
⎮ 1 1
dξ → ⋅ ω ⋅
⎮ s⋅ 2 ⋅ π ⎢ 2 ⎥ 2 s
⎮ ⎣ 2⋅ ω ⎦

This worksheet was prepared by Gilberto E. Urroz, March 2006

Numerical integration - page 9

Anda mungkin juga menyukai