Course Instructor Dr. Raymond C. Rumpf Office: A 337 Phone: (915) 747 6958 E Mail: rcrumpf@utep.edu Topic 6a umerical Integration EE 4386/5301 Computational Methods in EE Outline Introduction Discrete Integration Trapezoidal Integration Simpson s Integration Multiple Integrals Convergence 1
Introduction 3 Why Use umerical Integration? How to we calculate the following integral? b x e dx a o analytical solution exists to perform this integration by hand. We are forced to calculate the integration by other means. 4
Discrete Integration 5 Problem Setup Suppose we have a function f(x) and we want to integrate it from a to b. b a f x dx 6 3
Solution (1 of ) When we solve this problem on a computer, we will most likely only know the function value at discrete points. 7 Solution ( of ) A simple approach to approximate this integral is to represent the area under this function as a series of rectangles. Observe that the position of the points are at the center of the rectangles. b a ba ba f xdx f xnx f xn x n1 n1 x 8 4
Error Approximating the integral this way produces some error. Gaps between the true curve and the rectangles leads to error. 9 Reducing the Errors 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! 10 5
Trapezoidal Integration 11 Problem Setup Suppose we have a function f(x) and we want to integrate it from a to b. b a f x dx 1 6
Solution (1 of ) A more accurate technique for numerical integration uses the trapezoidal rule. For this, we place the points x n differently. Points are placed at the extreme ends and distributed evenly. 13 Solution ( of ) Instead of fitting rectangles under the curve, we use trapezoids. This conforms more closely to the curve. 14 7
Error Approximating the integral this way still produces some error. There is noticeably less error than with discrete integration. E 1 t x f x ba 1 The error for trapezoidal integration is 3 15 Formulation (1 of ) The total area of a trapezoid is f x A 1 1 x x f f baseheight tri 1 1 f x 1 widthheight A x x f rect 1 1 x1 x Total Area of Trapezoid A Arect At ri 1 f f x x x x f x x f f 1 1 Slide 16 1 1 1 1 8
Formulation ( of ) In trapezoidal integration, we add all of the areas of the trapezoids to approximate the integration. b a f x n1 x dx x n1 n1 x f n n f f n n1 f n1 nonuniform spacing uniform spacing Slide 17 Uniform Spacing When the spacing is uniform, trapezoidal integration reduces to b a x f x dx fn f n1 n 1 To understand this more deeply, we expand the summation over four trapezoids. n1 f f f f f f f f f f n n1 1 3 3 4 4 5 We see that each point is included twice, except the two endpoints at x = a and x = b. n1 f f f f f f f n n1 1 3 4 5 Slide 18 9
Discrete Vs. Trapezoidal Integration (1 of ) There are some key differences between discrete and trapezoidal integration: Points are distributed differently. Discrete integration is easier to implement. Trapezoidal integration has less error. Trapezoidal more elegantly handles nonuniform spacing. Discrete Integration Trapezoidal Integration 19 Discrete Vs. Trapezoidal Integration ( of ) Let s compare the equations for both discrete and trapezoidal integration. First, we can rearrange trapezoidal integration as follows: b a x f x dx fn f x f f f f f 0.5 0.5 n1 n1 1 3 4 5 The equivalent equation for discrete integration is b a f x dx x f x f f f f n1 n 1 3 4 We see that trapezoidal integration reduces to discrete integration but with one extra rectangle added. 0 10
Interpreting Trapezoidal Integration as Discrete Integration Trapezoidal integration can be written as b a x f x dx fn f x f f f f f 0.5 0.5 n1 n1 1 3 4 5 This can be interpreted as a modified discrete integration. 1 How Can Discrete & Trapezoidal Produce Roughly the Same Error? egative Error Positive Error Positive and negative error tend to cancel within a segment. 11
Simpson s Integration 3 Simpson s 1/3 Rule Suppose we have three adjacent points and we fit them to a second order polynomial. f x a a x a x 0 1 ow let s integrate the polynomial under the curve. x3 x3 0 1 f x dx a a x a x dx x1 x1 1 x f 4 f f 3 1 3 To implement Simpson s 1/3 rule, we simply apply this to f(x) in groups of 3 points. Slide 4 1
Derivation of Simpson s 1/3 Rule First, fit the three points to a polynomial. f x a a x a x 0 1 f f f f f a f a a x x 3 1 3 1 0 1 x 0 x Second, integrate the polynomial from x to x. x 1 1 3 3 3 a 3 0 a1xax dxa0x a1x ax a0x a x x x x Substitute in the expressions for a 0, a 1, and a. f f f 1 a x a x x x x f f f 3 3 x 3 3 4 3 3 1 0 f 1 3 Slide 5 Implementation of Simpson s 1/3 Rule Animation of umerical Integration Using Simpson s 1/3 Rule Slide 6 13
Simpson s 3/8 Rule This is similar to Simpson s 1/3 rule, except we apply it to f(x) in groups of 4 points. x4 x1 3 f x dx x f f f f 8 3 3 1 3 4 Slide 7 Multiple Integrals 8 14
Problem Setup Suppose we have a function f(x,y) with two independent variables. How do we evaluate a double integral? xb yd xa yc f x, y dxdy? We will view this as an integral of integrals. xb xa yd yc f x, ydy dx? We evaluate the inside integral for each step of in the integration of the outside integral. Slide 9 Illustration of umerical Double Integration We start with a D array. We numerically integrate each of the columns to get a 1D array. We numerically integrate the 1D array. Our final answer is the numerical double integral of the original D array. Slide 30 15
Via Discrete Integration This is very easy using discrete integration. Our discrete equation is xb yd M xa yc b a x M d c y,, f x y dxdy f a mx c ny xy m0 n0 The MATLAB code to do this is simply dx = (b - a)/m; dy = (d - c)/; I = sum(f(:))*dx*dy; Slide 31 Convergence 3 16
What is Convergence? Convergence is the tendency of a numerical algorithm to approach a specific value as the resolution of the algorithm is increased. This does OT imply the answer gets more correct. There may still be something wrong with your calculation! Slide 33 Demonstration of Convergence Suppose we wish to evaluate the following integral: sin xdx 0 How many segments are necessary? There is no way to tell. We must perform a convergence study! Slide 34 17
Analytical Answer To check ourselves, we can solve the integral analytically 0 sin xdx cos x 0 cos cos 0 Slide 35 Convergence Study Slide 36 18
Convergence Study It is up to you to decide when a numerical algorithm is sufficiently converged. Perhaps convergence happens here if only a rough estimate is needed. Perhaps convergence happens here if higher precision is needed. Slide 37 Convergence Does OT Imply Correctness Sometimes we get lazy and say that algorithms get more accurate with higher resolution. THIS IS OT CORRECT!!! Algorithms can only become better converged. Less Correct? More Correct Slide 38 19
Rule of Thumb for Resolution For calculations involving waves, the resolution begins to converge when you resolve one wave cycle with about 10 divisions. wavelength 10 Slide 39 0