Numerical Integration-Part 1

21
Slides Module 2.1: Numerical Integration - Part 1 Trapezoidal and Simpson’s Rule A NumFys Module http://www.numfys.net Fall 2012 A NumFys Module http://www.numfys.net Module 2.1: Numerical Integration - Part 1

description

Lecture on Trapezoidal and Simpsons Rule from NTNU

Transcript of Numerical Integration-Part 1

SlidesModule 2.1: Numerical Integration - Part 1Trapezoidal and Simpsons RuleA NumFys Modulehttp://www.numfys.netFall 2012A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.1 Matlab ModuleFirst Step:Save the corresponding Matlab code onto your computer andopen it in Matlab.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.2 Numerical Integration - Part 1The Task:How to determine a denite integral like

baf (x) dx, (1)if we cannot solve it analytically, i.e. if we cannot nd a functionF(x) withddxF(x) = f (x) (2)and

baf (x) dx= F(b) F(a). (3)A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.3 Numerical Integration - Part 1Example:We cannot determine the integral

10

x5+ e5xdx (4)analytically. So what to do?Lets plot the integrandf (x) =x5+ e5xforx [0, 1].A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.4 Numerical Integration - Part 1We know that the integral represents, in this case, the areaunder the graph. The area is well dened and nite.We could approximate it by a Riemann sum. For this purpose,we divide the interval [0, 1] into N 1 intervals of the samelength h withh =1N 1. (5)The endpoints of these intervals are located atxn= (n 1)h with n = 1, 2, 3, ..., N. (6)This leads naturally to the denition of a rectangle for eachinterval [xn, xn+1] whose height is determined by the value ofthe function at xn, i.e. f (xn).A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.5 Numerical Integration - Part 1For N= 6, we can sketch this in our previous plot.We can see that we make a fairly large error in our estimate ofthe integral when we use these rectangles. How can we dobetter?A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.6 Numerical Integration - Part 1One way to go forward is to make the intervals smaller. In thelimit when their length goes to zero, we should nd the exactvalue of the integral.However, this is impractical on the computer since it involvesinnitely many calculations. What else can we do?Well, a better approximation can already be obtained with ourlast set of intervals if we use trapezoids instead. Two sides (topand bottom) of each trapezoid are determined by the intervalalong the x-axis, while the other two sides (left and right) aredetermined by the values of the function f (x) at each end of theinterval.Lets see what this looks like in the plot.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.7 Numerical Integration - Part 1It seems that we are making a much smaller error than before,given the same set of intervals. The error seems to increasewhen the curvature of the graph increases (towards the right inthis plot).A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.8 Numerical Integration - Part 1Let us derive a formula for the area that the trapezoids cover.Using our notation above, the area of a trapezoid is given byAn= hfn + fn+12with fn= f (xn). (7)Here, h is the length of each interval, and hence the length ofthe base of each trapezoid, and the expressionfn + fn+12(8)represents the "average height" of the corresponding trapezoid.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.9 Numerical Integration - Part 1Adding up all the trapezoids, we obtain the total areaA = A1 + A2 + A3 + ... + AN1(9)and soA = hf1 + f22+ hf2 + f32+ hf3 + f42+ ...hfN1 + fN2. (10)Factoring out h and rearranging terms givesA = h

12f1 + f2 + f3 + ... + fN1 + 12fN

. (11)This formula describes the trapezoidal rule and approximatesthe original integral.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.10 Numerical Integration - Part 1The factors inside the bracket are 0.5, 1, 1, ..., 1, 0.5. This stemsfrom the fact that we only use the endpoints 0 and 1 once in ourcalculation while all other points are involved twice in thedetermination of trapezoidal areas.Note that we do not need to assume an interval [0, 1]. Instead,we can use this method for any interval [a, b]. The denitionsthen change toh =b aN 1and xn= a + (n 1)h. (12)This way, we can approximate the integral by

baf (x) dx h

12f1 + f2 + f3 + ... + fN1 + 12fN

. (13)A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.11 Numerical Integration - Part 1It can be shown that this method contains an error (i.e. actualvalue of the denite integral vs. numerical value) that isproportional to1 (b a)3,21N2and3 the (maximum) magnitude of the second derivative of f (x)over the respective interval.Hence, doubling the amount of intervals reduces the error by afactor of four! However, choosing very large N is not a goodidea for two reasons:i) It increases the computational time.ii) The computer has nite precision; rounding errors becomeimportant!A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.12 Numerical Integration - Part 1The second derivative is linked to the curvature of the graphand we saw above that the error indeed increased where thecurvature was larger. This makes sense since we are trying toapproximate a curved graph with a straight line.In the enclosed Matlab script, we approximate the integral

10

x5+ e5xdx (14)for various N. As we increase N, the values for the trapezoidalrule converge.Try it yourself!Choose N= 5, 10, 20, 50, 100, 250, 500.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.13 Numerical Integration - Part 1Question:Is there a better, i.e. more precise, method to approximate anintegral? Can we do just as well with fewer intervals?Answer:Yes, there are several methods.One method is called Simpsons rule. The idea behind thismethod is that we do not approximate the graph locally by astraight line, as is the case for the trapezoidal method, butrather by a polynomial of order 2. This means that weapproximate the function f (x) in each subinterval byf (x) cnx2+ dnx+ en=: gn(x). (15)The parameters cn, dn, en are chosen so that f (x) and gn(x)coincide at the endpoints of the interval and at its midpoint.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.14 Numerical Integration - Part 1Example:Let us plot our original function and approximate it by asecond-order polynomial with the above characteristics.We see that the polynomial describes the original functionalready quite well. And we have not even started to divide theinterval [0, 1] into subintervals...A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.15 Numerical Integration - Part 1It is important to note that Simpsons rule requires and oddnumber of points (N= 3, 5, 7, ...) along the x-axis since weneed to use three points for each interval, i.e. left endpoint,right endpoint and midpoint.At the same time, two adjacent intervals share one endpoint,i.e. the right endpoint of one interval coincides with the leftendpoint of another interval (except at x= N).All along, we still maintain a uniform division of the originalinterval [a, b] into smaller subintervals.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.16 Numerical Integration - Part 1The simplest case is using three points for the original interval,representing one interval with one midpoint:x1= a, x2= (a + b)/2, x3= b. (16)This case is exhibited in the previous plot. In this case, it can beshown that the integral approximation reduces to

baf (x) dx b a6[f (x1) + 4f (x2) + f (x3)] . (17)The next case uses ve points: two intervals with one midpointeach. Now, we obtain

baf (x) dx h3 [f (x1) + 4f (x2) + 2f (x3) + 4f (x4) + f (x5)] (18)where h is the distance between two successive points alongthe x-axis. This scenario is plotted on the next slide.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.17 Numerical Integration - Part 1The case of two intervals:[0, 0.5] and [0.5, 1]We have a total of 5 points along the x-axis, namely 3 intervalendpoints (x1= 0, x3= 0.5, x5= 1) and 2 midpoints(x2= 0.25, x4= 0.75). Simpsons rule employs second-orderpolynomials in each interval.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.18 Numerical Integration - Part 1The general expression for an arbitrary amount of intervals is

baf (x) dx h3 [f (x1) + 4f (x2) + 2f (x3) + 4f (x4) + ... + 4f (xN1) + f (xN)](19)or, using our previous notation above,

baf (x) dx h3 [f1 + 4f2 + 2f3 + 4f4 + ... + 4fN1 + fN] . (20)Let us use Matlab again to compute the same integral as for thetrapezoidal rule, only this time we use Simpsons rule.Simply uncomment the corresponding lines in the enclosedMatlab script!A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.19 Numerical Integration - Part 1Try it yourself!Choose N= 3, 5, 7, 9, 11, 13, 15.Does it converge faster than before?A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1Slidesp.20 Numerical Integration - Part 1In contrast to the trapezoidal rule, Simpsons rule contains anerror that is proportional to1 (b a)5,21N4and3 the (maximum) magnitude of the fourth derivative of f (x)over the respective interval.In other words, it is superior in accuracy to the trapezoidal rulewith respect to the amount of intervals N.A NumFys Module http://www.numfys.netModule 2.1: Numerical Integration - Part 1