Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 33 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
33
Dung lượng
861,06 KB
Nội dung
Numerical Integration Concepts Numerical integration is used to evaluate a definite integral when there is no closed-form expression for the integral or when the explicit function is not known and the data is available in tabular form only. Numerical integration (or quadrature) consists of methods to find the approximate area under the graph of the functionflx) between two x values. The simplest of these uses the trapezoid rule. If we divide the area under the curve into a sufficiently large number of parts, as shown in Figure 1 I. 1, then the area under the curve (the approximate integral) is given by: Figure 11.1 (1 1.1) 1. X=b X We approximate the representative strip to a trapezoid. For a clearer drawing, only five strips are used. Obviously more, smaller, strips are needed for a good approximation. Let there be n strips and hence n+ 1 data points. The area of a typical strip is: 222 A Guide to Microsoft Excel 2002 for Scientists and Engineers Yi +Yi+l 2 Ai =Ax (11.2) Combining the two equations we see that: (1 1.3) Yl +Y2 +&22A+ +h Yn + Yn+l 2 2 I=Ax- 2 Giving: Ax (1 1.4) 2 I = - (y, + 233 + 2y3 + 2Y4 + **. + 2Yn + Yn+l ) or (11.5) Equation 1 1.5 is called the trapezoid rule. We use the trapezoid approximation in the first exercise to evaluate an integral. A better approximation to the integral is obtained by taking two adjacent strips and joining the three points on the curve with a parabola. This gives Equation 1 1.6, called the Simpson one-third rule for approximating the area under a curve. 1 n=2 1 = - C (Y, +4x+, +Y,+Z)~ (1 1.6) 3 k1.3.5 This rule requires that there be an even number of equally spaced strips. The second exercise uses this approximation. Approximating the curve through four adjacent points to a cubic equation gives the Simpson %rule shown in Equation 1 1.7. (11.7) Surprisingly, the rule is often less accurate than the ‘/3 rule. However, unlike the ’/3 rule, it does not require an even number of strips. This is an advantage when the data is available only in tabular form (the explicit function being unknown) and there is an even number of data points - an odd number of strips. Accuracy If the interval a to b is divided into successively more strips then, in principle, the accuracy should increase. This is not so in Numerical Integration 223 Exercise 1 : The Trapezoid Rule practice. When the number of strips becomes very large, the accumulated round-off error becomes significant. In some of the exercises and problems, we evaluate definite integrals with known values. There are two reasons for doing this: we can compare our approximations with the known values to check the accuracy, and it will give us confidence to attack integrals with unknown values. Using the trapezoid approximation, we evaluate the integral ix sin(x)h 0 and show that the approximation yields a result close to the exact result of TC. Looking at Figure 1 1.1 and Equation 1 1.3, we see that when five strips are used a total of sixAx) ory-values are needed. In general, when N strips are used then n+l values of y are required. We start this exercise with IO strips so we need 1 1 values ofy. For this exercise we will use the trapezoid rule in the form of Equation 11.3. When you have completed this exercise your worksheet should resemble that in Figure 1 1.2. (a) Open a new workbook and enter the text in AI :A6. Enter the following: B3: 0 The lower limit of the integration B4: =PI() The upper limit of the integration B5: 10 The number of strips B6: =(upper-lower)/BS The last formula computes the value of Ax which we use to find the 1 1 x values: lower, lower + Ax, lower + 2hX. etc. (b) Select A3:B6 and use insertlName(Create to name the cells in B3:B6. (c) Enter the text in row 8. (d) Enter the following formulas: A9: =lower The value of x,, A 10: =AS+delta The value of x, Copy these down to row 19 to give the 1 1 x values. 223 A Guide to Microsoft Excel 2002 for Scientists and Engineers (e) In B9 enter the formula =A9*SIN(A9) to compute the value of y,. Copy this formula down to B 19. This gives the 1 1 y values for the strips. Note how using the Equation I I .3 form of the trapezoid rule allowed us simply to copy B9 to the other cells. I A I B I C 1 ITrapetoid Rule 3.141 593 0.314159 Figure 11.2 (9 In C9 enter the formula =delta*(BS+BlO)R to compute the area of the first strip. Copy this formula down to C 18 to get the areas of the other nine strips. Be careful NOT to copy it to C19. (g) Enter the text in B20:B22. (h) Enter the following formulas: C20: =SUM(CS:C18) c21: =PI() c22: =( c20-cz1 yc21 To sum the IO trapezoid areas The exact result for the integral The last formula computes the error in the approximation. (i) Save the workbook as CHAP1 1 .XLS. Numerical Integmtion 225 1 2 3 Exercise 2: Simpson's 1/3 Rule A I B C D Simpson's 1/3 Rule lower 0 If all has gone well you will see that the trapezoid rule approximates the integral to 3.1 1571 1, which is 0.82% low compared to the exact value. You may wish to modi@ the worksheet to use 20 strips and note how the error is reduced to - 0.2 1 Yo. 4 5n 6 7 This exercise uses Simpson's V3 rule to find the approximate value of ]exp(x')k 0 - upper 2 20 delta 0.1 There is no analytical method for this integral but the published value for the interval [0,2] is 16.452627. For improved accuracy we will use 20 strips; which means we need 21 x,y pairs. When completed, your worksheet should resemble Figure 1 1.3. 171 1.61 12.935821 110,4428 1.71 17.99331 1 Figure 11.3 (a) Open the workbookCHAPl1 .XLS and on Sheet2 enterthe text in A1 :A6. 226 A Guide to Microsoft Excel 2002 for Scientists and Engineers (b) Select A3:B6 and use the command lnsertlNamelCreate to name the cells in B3:B6. (c) Enter the following values or formulas: B3: 0 The lower limit of the integration B4: 2 The upper limit of the integration B5: 20 The number of strips B6: =(upper-lower)/n The formula in B6 computes the value of kx which we use to find the 2 I x -values: lower, lower +Ax, lower +2Ax, etc. (d) Enter the text in row 8. (e) The numbers in A9:A29 are not essential but may help you understand how the Simpson rule is implemented in the worksheet. Enter these using the Fill Series method you learnt in an earlier chapter. (f) Enter the initial x and y values in B9 and C9: B9: =lower The value of x, C9: =EXP(B9"2) The value ofy, (g) Enter the second and subsequent values of x and y: B 10: =BS+delta The value of x2 C10: =EXP(B10A2) The value ofy, Copy B I0:C IO down to row 29. When the values of Ax values are constant Equation 1 1.6 becomes: (1 1.8) Thus we may compute each of the terms, find the summation and multiply the result by AxL? to approximate the integral. The first term in the summation is (y, + 4y, +y3) so in D9 enter the formula =C9 + 4*C10 + Cll. Check that your value agrees with that in Figure 1 1.3. (h) We need to copy this formula to each alternate cell in the range Dl 1 :D27. The easiest way to do this is select D9:DlO and drag the fill handle down to D27. Numerical Integration 227 (i) Enter the text in C30 and in D30 enter =SUM(DS:D29)"delta/3 to compute the integral. ('j) Enter appropriate text and formulas in C3 1 :D32 to show the published value and compute the percentage error. (k) Save the workbook CHAP1 1 .XLS, as we will use it in the next exercise. Exercise 3: Adding In the last exercise we developed a worksheet to evaluate a certain integral. Do we need to repeat all that work if we wish to evaluate another? We could, of course, edit the formula in C9 to reflect the new function to be integrated and copy this down to C29. Another way is to put the function in a module sheet and change the user- defined function each time we wish to evaluate a different integral. Flexibility (a) Open the workbook CHAP1 1.XLS. We wish to duplicate Sheet2. Hold down the key and drag the Sheet1 tab to the right (you will see an icon of a sheet of paper overprinted with a -k sign). Release the mouse button and tab labelled Sheet2 (2) will appear. Delete Sheet3 and rename Sheet2 (2) as Sheet3. (b) Open the Visual Basic Editor with the command ToolsJMacrol - Visual Basic Editor or with the @+[F111 shortcut. Insert a macro sheet of the CHAP1 1 project. Type the user-defined function shown below. 'Function to use with Simpson Rule worksheet Function Simp(x) End Function Simp = Exp(x * 2) Remember, whenever you edit a user-defined function, you must recalculate the worksheet by pressing [F91 before any changes in the function will take effect. (c) Return to the worksheet containing the Simpson rule calculation. Change the formula in C9 to =SIMP(B9) and copy this down to C29. The values should stay the same as before (see Figure 1 1.3). (d) Now we will make a quick change to evaluate 1 J exp( -2 )& -I 228 A Guide to Microsoft Excel 2002 for Scientists and Engineers v vi11 not be reflected in vorksheet until something happ . t P I __ , ,_A. - . . . . . . . - . . . . vorksheet. Pressing@ will ca his, as will changing the values he limits. Change the third line in the module function to read Simp = Exp(-(x A 2)). Carefully note the position of the negation operator relative to the parentheses. The change made in the module \ the \. ens to mate r.xcei recalculate the \. use t for t (e) Return to the Simpson worksheet. Change the values of lower and upper to - 1 and 1, respectively. The value of the new function has been calculated. Remember, you may have to change the cell with the published value. For this function, in the interval - 1 to 1, the result should be approximately 1.49. (f) To make another test, change the user-defined function to Simp = x * Sin(x). Return to the Simpson worksheet and change the values of lower and upper to 0 and PI(), respectively. How does your result compare to that in Exercise l? (g) Save the workbook. Exercise 4: Going In this exercise we implement the Simpson '/3 rule as a user- defined function in a module. To check our function more easily we find the value of the same integral as in Exercise 1. In addition, we experiment with making the strip successively smaller and observe how the percentage error changes. Modular (a) Open the Visual Basic Editor with the @+@ shortcut. On the module sheet for CHAP1 1 project, code the integrating function and the function to be integrated as in Figure I I .4. Do not type the line numbers; they are for discussion purposes only. The statements in the Integral function are examined at the end of the exercise. (b) On Sheet4 of the CHAP1 1 .XLS workbook enter the text and values shown in A1 :A1 0 of Figure 1 1.5. (c) Select A3:B5 and, using InsertlNamel(J-eate, name the cells B3:B5. (d) The formulas and values to be entered in B3:BlO are: B3: 0 The lower limit B4: =PI() The upper limit B5: =PI() The known value of the integral B7: 10 The number of strips B8: =Integral( lower,upper, B7) B9: =B8-exact Error calculation Value from Simpson's rule Numerical Integration 229 B 10: =BS/exact Percentage error Format B10 to show a percentage value with four decimal places. (e) Check that your results in B8:BlO agree with Figure 1 I .5. If they do not, you may need to edit your module or your formulas. 1 'Simpson One-Third Rule Approximation 2 Function integral(a, b, n) 3 Integral = O# 4 delta = (b - a) I n 5 x=a 6 For i = 1 To n Step 2 7 a 9 IO Nexti 11 12 End Function 13 14 'The function to be integrated 15 Function y(x) 16 17 End Function Term = y(x) + 4 * y(x + delta) + y(x + 2 * delta) Integral = Integral + Term x = x + 2 * delta Integral = Integral * delta I3 y = x * Sin(x) Figure 11.4 Figure 11.5 (f) Select B7:B IO and drag the handle to column F. Change your n values to match those in the spreadsheet. Note that large numbers may be entered with a comma to make them more readable. Do not be surprised if your worksheet takes some time to respond. Microsoft Excel has to do a large number of calculations. Save the workbook. [...]... 8 I 81 9 = 0 .88 888 88 888 88 889 519 = 0.55555 55555 55555 0.65214 515 48 62546 0.34 785 484 5 1 37455 0.5 688 8 88 888 88 889 0.4 786 2 86 704 99334 0.23692 688 50 56 189 0.46791 39345 72691 0.36076 15730 48 139 0.17132 44923 79170 0.362 68 3 783 3 783 63 0.3 1370 664 58 7 788 7 0.222 38 10344 53966 0.10122 85 362 90617 0.29552 42247 14753 0.26926 67 I93 09997 0.2 I9 08 63625 15 982 0.14945 13491 50 581 0.06667 13443 086 48. .. 2 48 A Guide to Microsoft Excel 200 2for Scientists and Engineers (b) On Sheet3 enter the text and values shown in A1 :G7 of Figure 12.5 Select the range B4:C6 and use InsertlName(Createto name the cells C4:C6 (c) Enter the series of values in A8 :A 13 (d) Enter these formulas: The initial x value B8: =XO C8: =YO The initial y value The k parameters D8: =h*rkfunc(B8,C8) E8: =h*rkfunc ((B8 + h/2), (C8... help Alternatively, we could record the result for, say, 20 recalculations and take an average This could be done automatically using a VBA subroutine (f) If you have set the calculation method to manual, reset it to automatic Save the worksheet 2 38 A Guide to Microsoft Excel 2002 f o r Scientists and Engineers Problems 1 Modify the worksheet of Exercise I to use 20 strips Note how the resulting value... provide a simple way to illustrate a Monte Carlo calculation A large worksheet would be needed to model a true stochastic process The author’s web page has a workbook demonstrating Brownian motion 236 A Guide to Microsoft Excel 200 2for Scientists and Engineers Consider a circle inscribed within a square with sides of I units The radius (Y) of the circle will be 1/2 A large number (N)of darts are randomly... the dart The function returns a value from 0 to 1, so for the x value we will use RAND()*10 and for the y value RAND()*200 (b) In A3 enter =RAND()*lO and in B3 enter =RAND()*200 Copy these down to row 1002 Your values will not be the same as in the figure You may see a message such as Calculation Cells 49% on the status bar If your PC reacts slowly, use ToolslQptions and on the Culculations tab set... 246 A Guide to Microsoft Excel 2002 for Scientists and Engineers In Exercise 4 of Chapter 13 a userdefined function is developed to perform the Runge-Kutta computation This may look somewhat formidable so let us see how we can put it into a worksheet We have seen in Exercise 1 how to evaluate the equivalent to k,;this is the value of the differential function for various x andy values The second parameter,... set the method of calculation to manual A status bar message of Calculate will to recalculate the worksheet remind you when to press Numerical Integration 23 7 (c) The formula in C3 is =IF(B3> -A3 "3+10 *A3 A2+5 *A3 ,0,1) This returns 0 when the dart has fallen above the curve, and 1 otherwise Copy it down to (21002 (d) The formulas in column F are: Total darts thrown F2: =COUNT (A3 :AI002) Darts inside the curve...230 A Guide to Microsoft Excel 200 2for Scientists and Engineers Note how the absolute error progressively decreases up to n = 10,000 and then increases for larger n values Here we are seeing the accumulated round-off errors beginning to creep in Explanation of the Integral function: Line Comment 3 Type this as Integral = 0.0 to tell VBA that it is a real, not an integer, value This initializes the value... equation m-=g kv’ d v dt (ii) (12.1) Consider the chemical reaction A + B + C where the rate of reaction is proportional to the concentration of A and to the concentration of B Let x be the amount of A and B reacted at time t, and let the initial concentration of A and B be a and b, respectively These quantities will be related by G k -= k, ( a- x)(b -x) dt (1 2.2) (iii) The equation of motion for a harmonic... with any data but is the least accurate requires an even number of equally spaced strips requires equally spaced x values, may be less accurate than the 'A rule Suppose we have 63 strips (Le 64 data pairs) We may use the 3s / rule for the first three and the ' rule for the remaining 60 strips / 3 We have seen that increasing the number of strips improves the accuracy of these approximations With tabular . 0.5 688 8 88 888 88 889 0.5 384 6 93101 05727 0.4 786 2 86 704 99334 0.906 17 984 59 386 64 0.23692 688 50 56 189 0.2 386 1 9 186 0 83 197 0.66 120 9 386 4 66264 0.46791 39345 72691 0.36076 15730 48 139. 20 recalculations and take an average. This could be done automatically using a VBA subroutine. (f) If you have set the calculation method to manual, reset it to automatic. Save the. 0.57735 02691 89 626 3 0 81 9 = m= 519 = 0 .88 888 88 888 88 889 0.77459 66692 41 483 0.55555 55555 55555 4 0.339 98 10435 84 856 0.65214 515 48 62546 0 .86 1136311594052 0.34 785 484 5 1 37455