11/4/2019 1 Computational Science: Computational Methods in Engineering Numerical Integration Outline • Introduction • Discrete Integration • Trapezoidal Integration • Simpson’s Integration • Multiple Integrals • Convergence 2 1 2
11/4/2019
1
Computational Science:
Computational Methods in Engineering
Numerical Integration
Outline
• Introduction•Discrete Integration• Trapezoidal Integration• Simpson’s Integration
•Multiple Integrals
•Convergence
2
1
2
11/4/2019
2
Slide 3
Introduction
Why Use Numerical Integration?
4
How is the following integral evaluated?
2
?b
x
a
e dx No analytical solution exists to perform this integration by hand.
The integration must be calculated by other means.
3
4
11/4/2019
3
Slide 5
Discrete Integration
…also called a Riemann Sum
Problem Setup
6
Suppose there exists a function 𝑓 𝑥 and it is to be integrated from a to b.
b
a
f x dx
5
6
11/4/2019
4
Solution (1 of 2)
7
When this problem is solved on a computer, it is most likely that the function value is only known at discrete points.
Solution (2 of 2)
8
A simple approach to approximate this integral is to represent the area under this function as a series of rectangles.
1 1
b N N
n nn na
b a b af x dx f x x f x x
N N
Observe that the position of the points are at the center of the rectangles.
x
7
8
11/4/2019
5
Error
9
Approximating the integral this way produces some error. Gaps between the true curve and the rectangles leads to error.
Reducing the Errors
10
The only way to reduce error is to use thinner rectangles. However, this increases the number of computations that have to be performed which increases calculation time and could lead to larger round‐off error.
Or, use a different numerical integration technique altogether!
9
10
11/4/2019
6
Terminology
Slide 11
Left‐Hand Riemann Sum
Center Riemann Sum
Right‐Hand Riemann Sum
Slide 12
Trapezoidal Integration
11
12
11/4/2019
7
Problem Setup
13
Suppose there exists a function f(x) that is to be integrated from a to b.
b
a
f x dx
Solution (1 of 2)
14
A more accurate technique for numerical integration uses the trapezoidal rule. For this, we place the points xn differently. Points are placed at the extreme ends and distributed evenly.
13
14
11/4/2019
8
Solution (2 of 2)
15
Instead of fitting rectangles under the curve, use trapezoids. This conforms more closely to the curve.
Error
16
Approximating the integral this way still produces some error. There is noticeably less error than with discrete integration.
The error for trapezoidal integration is 31
12tE x f x b a
15
16
11/4/2019
9
Formulation (1 of 2)
Slide 17
The total area of a trapezoid is
tri 2 1 2 1
1 1base height
2 2A x x f f
rect 2 1 1width heightA x x f
rect t
2 1 1 2 1 2 1
1
ri
22 1
1
2
2
A
x x f x x f
A A
f
f fx x
Total Area of Trapezoid
1x 2x
1f x
2f x
Formulation (2 of 2)
Slide 18
In trapezoidal integration, all of the areas of the trapezoids are added together to approximate the integration.
11
1
11
nonuniform spacing2
uniform spacing 2
Nn n
n nbn
Na
n nn
f fx x
f x dxx
f f
17
18
11/4/2019
10
Uniform Spacing
Slide 19
When the spacing is uniform, trapezoidal integration reduces to
112
b N
n nna
xf x dx f f
To understand this more deeply, we expand the summation over four trapezoids.
1 1 2 2 3 3 4 4 51
N
n nn
f f f f f f f f f f
We see that each point is included twice, except the two endpoints at x = a and x = b.
1 1 2 3 4 51
2 2 2N
n nn
f f f f f f f
Discrete Vs. Trapezoidal Integration (1 of 2)
20
There are some key differences between discrete and trapezoidal integration:
Discrete Integration Trapezoidal Integration
• Points are distributed differently.• Discrete integration is easier to implement.• Trapezoidal integration has less error.• Trapezoidal more elegantly handles nonuniform spacing.
19
20
11/4/2019
11
Discrete Vs. Trapezoidal Integration (2 of 2)
21
Compare the equations for both discrete and trapezoidal integration. First, trapezoidal integration can be rearranged as follows:
1 1 2 3 4 51
0.5 0.52
b N
n nna
xf x dx f f x f f f f f
The equivalent equation for discrete integration is
1 2 3 41
b N
nna
f x dx x f x f f f f
It can be observed that trapezoidal integration reduces to discrete integration but with one extra rectangle added.
Interpreting Trapezoidal Integration as Discrete Integration
22
Trapezoidal integration can be written as
1 1 2 3 4 51
0.5 0.52
b N
n nna
xf x dx f f x f f f f f
This can be interpreted as a modified discrete integration.
21
22
11/4/2019
12
How Can Discrete & Trapezoidal Produce Roughly the Same Error?
23
Negative Error
Positive Error
Positive and negative error tend to cancel within a segment.
Slide 24
Simpson’s Integration
23
24
11/4/2019
13
Simpson’s 1/3 Rule
Slide 25
Suppose we have three adjacent points and we fit them to a second‐order polynomial.
3 3
1 1
20 1 2
1 2 3
14
3
x x
x x
f x dx a a x a x dx
x f f f
20 1 2f x a a x a x
Now let’s integrate the polynomial under the curve.
To implement Simpson’s 1/3 rule, we simply apply this to f(x) in groups of 3 points.
Derivation of Simpson’s 1/3 Rule
Slide 26
First, fit the three points to a polynomial.
32 2 30 1 2 0 1 2 0 2
1 1 22
2 3 3
xx
x x
a a x a x dx a x a x a x a x a x
20 1 2f x a a x a x
Substitute in the expressions for a0, a1, and a2.
x 0 x 3 1 3 2 1
0 2 1 2 2
2
2 2
f f f f fa f a a
x x
Second, integrate the polynomial from –x to x.
3 3 2 10 1 2 322 2
322 2 12 2
3 324
3
f f ff
xa x a x x x x f f f
25
26
11/4/2019
14
Implementation of Simpson’s 1/3 Rule
Slide 27
Animation of Numerical Integration Using Simpson’s 1/3 Rule
Simpson’s 3/8 Rule
Slide 28
This is similar to Simpson’s 1/3 rule, except it is applied to f(x) in groups of 4 points.
4
1
1 2 3 4
33 3
8
x
x
f x dx x f f f f
27
28
11/4/2019
15
Slide 29
Multiple Integrals
Problem Setup
Slide 30
Suppose the function f(x,y) has two independent variables.
How is a double integral evaluated?
, ?y dx b
x a y c
f x y dxdy
Think of this as an “integral of integrals.”
, ?y dx b
x a y c
f x y dy dx
Evaluate the inside integral for each step of in the integration of the outside integral.
29
30
11/4/2019
16
Illustration of Numerical Double Integration
Slide 31
The final answer is the numerical double integral of the original 2D array.
Start with a 2D array.
Numerically integrate each of the columns to get a 1D array.
Numerically integrate the 1D array.
Via Discrete Integration
Slide 32
This is very easy using discrete integration. The discrete equation is
0 0
, ,y dx b M N
m nx a y c
f x y dxdy f a m x c n y x y
b ax
Md c
yN
The MATLAB code to do this is simply dx = (b - a)/M;dy = (d - c)/N;I = sum(f(:))*dx*dy;
31
32
11/4/2019
17
Slide 33
Convergence
What is Convergence?
Slide 34
Convergence is the tendency of a numerical algorithm to approach a specific value as the resolution of the algorithm is increased.
This does NOT imply the answer gets more correct.
There may still be something wrong with your calculation!
33
34
11/4/2019
18
Demonstration of Convergence
Slide 35
Suppose the following integral is to be evaluted:
0
sin ?xdx
How many segments are necessary?
There is no way to tell. A convergence study must be performed!
Analytical Answer
Slide 36
To check the final answer, it is possible here to solve the integral analytically…
00
sin cos
cos cos 0
2
xdx x
35
36
11/4/2019
19
Convergence Study
Slide 37
Convergence Study
Slide 38
Perhaps convergence happens here if only a rough estimate is needed.
Perhaps convergence happens here if higher precision is needed.
It is up to you to decide when a numerical algorithm is sufficiently converged.
37
38
11/4/2019
20
Convergence Does NOT Imply Correctness
Slide 39
Less Correct
More Correct?
Sometimes people get lazy and say that algorithms get more accurate with higher resolution.
THIS IS NOT CORRECT!!!
Algorithms can only become better converged.
Rule‐of‐Thumb for Resolution
Slide 40
For calculations involving waves, the resolution begins to converge when you resolve one wave cycle with about 10 divisions.
wavelength 10
39
40