Numerical Analysis – Lab 6 Introduction to Integration Goals The goals of this lab are 1. to introduce ideas of integration and differential equations, 2. to review Taylor series 3. to revisit rate of convergence Two difficult functions This week and next we will try to deal with a couple of difficult functions, sin x2 and h( x) e x . The first function is chosen just to be obnoxious, but the g ( x) e second function is a form of the function for the normal “bell” curve used so often in probability and statistics. We plan to integrate them. Neither function can be integrated in what we call “closed form,” that is, in terms of powers, roots, sines, cosines, exponentials and logarithms. Hence, we turn to 2 numerical methods to evaluate their integrals. In particular, we want has a value between 4 and 5, and 1 3 0 g ( x)dx , which h( x)dx , which has a value between ½ and 1. 0 We’ll be happy with three significant figures. For my examples, I’ll use f ( x) e x and try to integrate this from 0 to 2. I know that the example should be exactly 2 0 e x dx e2 e0 , which is approximately 6.389056. Irrelevant and distracting warm-up Question 1: Assume the equator is a perfect circle of circumference exactly 40,000 kilometers, and that you want to divide this by to get its radius. You want your answer to the nearest millimeter. How many decimal places of do you need to use? Question 2: Assume that the universe is a perfect sphere with radius exactly 6 billion light years. How many decimal places of do you need to use to calculate the circumference of the universe to the nearest millimeter? Show your work. Way 1 – midpoint method When we learned about integration, we learned to write a definite integral as a Riemann sum of the areas of rectangles. We divide the interval unto a bunch of sub- Lab 6 Integration 1 intervals. For each sub-interval, say [ xi 1 , xi ] , i = 1, …,n, we find pick, arbitrarily, a point in that interval, say xi* , and find f xi* . We also denote by xi the length of that interval, xi xi xi 1 . Then we evaluate the so- f x x n called Riemann sum, * i 1 i i . Then, we take i 1 2 3 4 5 6 x(i-1) 0.000 0.333 0.667 1.000 1.333 1.667 x(i) 0.333 0.667 1.000 1.333 1.667 2.000 the limit as the length of the largest interval goes to zero, and, if that limit exists, we call it the Integral. Here, we will take n intervals of equal length, and for our arbitrary points xi* , we will take Fig. 1 – setting up my points the midpoints of each interval. For my function, I’ve taken n = 6, and, since my interval is [0,2], I get the six intervals shown in my text box at the right. Note, I used “real” thirds, but am only i x(i-1) x(i) midpoint f(x*) displaying three decimal places. 1 0.000 0.333 0.167 1.18136 1 2 0.333 0.667 0.500 1.648721 Also, my x . 3 3 0.667 1.000 0.833 2.300976 Next, I find the 4 1.000 1.333 1.167 3.211271 midpoints, and evaluate my 5 1.333 1.667 1.500 4.481689 function, f ( x) e x , at these 6 1.667 2.000 1.833 6.254701 midpoints. I sum these values, Sum 19.07872 (19.07872) then multiply by my Sum*Delta 6.359573 interval length (1/3) and get my Fig 2 – evaluating and summing estimated answer, 6.359573, which I probably should round to about two decimal places, 6.36. This compares fairly well with the actual value of about 6.38906. This is to be expected, since f ( x) e x is, in a sense, one of the very nicest functions there is. Task 1 – Use the Midpoint method to estimate 3 0 g ( x)dx and 1 h( x)dx . 0 Start with six intervals, and see how your estimates change as the number of intervals increases. You might also want to see how it behaves on 2 0 e x dx , since you know the answer to that, and you don’t know the answer to either of your integrals. Info and hints – To six significant figures, the correct values are 4.55453 and .746824. I only want to see your first (six-interval) spreadsheet. Summarize your other experiments. It makes more sense to increase your intervals by multiplying. Like, do something like 6, 60, 600, 6000 intervals (multiplying by 10), or 6, 24, 144, 576 (multiplying by 4). Lab 6 Integration 2 Way 2 – Trapezoid rule Geometrically, the midpoint rule is approximating the area of a region by using a staircase-shaped region. Such regions are called step functions. It should be evident that better kinds of regions are likely to give better approximations. The trapezoid rule uses a series of trapezoids (hence the name). This gives a series of straight-line approximations and approximates the region with a polygon. Mathematics books are full of pictures illustrating this idea. Burden and Faires i x(i) f(x(i)) Trap have one on page 188. You should look at 0 0.000 1.000 it, and compare it to the treatment you 1 0.333 1.396 1.198 probably saw in your Calculus I course. 2 0.667 1.948 1.672 No need to repeat it here. 3 1.000 2.718 2.333 Suffice it to say that, for each 4 1.333 3.794 3.256 interval, we assume that the curve and the 5 1.667 5.294 4.544 trapezoid have similar areas. Since the 6 2.000 7.389 6.342 area of the trapezoid is 1 Sum 19.344 f xi 1 f xi x , we can average 2 Sum*Delta 6.448105 the values of the endpoints of each Fig. 3 – Trapezoid rule interval, add them up, and multiply by the interval length. This is what the spreadsheet at the right does. We see that I remembered to display 3 decimal places this time, and that the estimate is actually worse than the midpoint rule. Recall that the actual value is about 6.38906, and our error has increased from about –0.03 to about +0.06. Good idea – Check my work here. Also, check my value of 6.38906. Task 2 – Use the trapezoid rule to estimate 3 0 g ( x)dx and 1 h( x)dx . 0 Start with six intervals, and see how your estimates change as the number of intervals increases. You might also want to see how it behaves on 2 0 e x dx , since you know the answer to that, and you don’t know the answer to either of your integrals. Info and hints – In your Conclusions, make sure that you compare these values to the ones you got for the midpoint rule, as well as to the ones you will get in subsequent tasks. Simpson’s Rule A polygon approximation is kind of jagged. Maybe parabolas would be better. This leads to Simpson’s Rule. It takes three values to determine a parabola, so we use two intervals at a time. The plan is to take three consecutive values of f(x), call them a, b and c, and find the parabola that goes through these three points, and then integrate this from xi to xi+ 2. Lab 6 Integration 3 Fortunately, there is a formula so that, after you’ve done it once, you can use the result and not have to do the actual work any more. Task 3 – do the work – Suppose that p(x) is a parabola and that p(-h) = r, p(0) = s and p(h) = t. 1. Find the equation for the parabola in the form p( x) ax 2 bx c . r 4s t 2h . 2. Integrate this from – h to h, and show that the integral is 6 3. Convince yourself that this is “scalable,” that is, given three consecutive values on a curve, f xi , f xi 1 and f xi 2 , i x(i) x(i+1) x(i+2) the integral of the parabola that 0 0.000 0.333 0.667 passes through the three points 2 0.667 1.000 1.333 is 4 1.333 1.667 2.000 Fig. 4 – the x-values for Simpson’s rule f xi 4 f xi 1 f xi 2 2x 6 Doing it on a spreadsheet Since we want to use two intervals at once, we want three points on each line of the spreadsheet. The spreadsheet might start as shown in Fig. 4. Then, we should calculate the value of f(x) at these data points, as shown in Fig. 5. i 0 2 4 x(i) 0.000 0.667 1.333 x(i+1) 0.333 1.000 1.667 x(i+2) 0.667 1.333 2.000 f(x(i)) 1.000 1.948 3.794 f(x(i+1)) 1.396 2.718 5.294 f(x(i+2)) 1.948 3.794 7.389 Fig. 5 – x-values and y-values for Simpson’s rule Now, calculate (a + 4b + c)/6, sum, and multiply by the interval length, as shown in Fig. 6: i 0 2 4 x(i) 0.000 0.667 1.333 x(i+1) 0.333 1.000 1.667 x(i+2) 0.667 1.333 2.000 f(x(i)) 1.000 1.948 3.794 f(x(i+1)) 1.396 2.718 5.294 f(x(i+2)) (a+4b+c)/6 1.948 1.421697 3.794 2.769088 7.389 5.393447 sum 9.584233 sum*2delta 6.389489 Fig. 6 – Simpson’s rule This is considerably more accurate. Lab 6 Integration 4 Task 4 – Use Simpson’s rule to estimate 3 0 g ( x)dx and 1 h( x)dx . 0 Start with six intervals, and see how your estimates change as the number of intervals increases. You might also want to see how it behaves on 2 0 e x dx , since you know the answer to that, and you don’t know the answer to either of your integrals. Taylor series methods The function h( x) e x is easy to analyze with Taylor series. We know (or at 2 x 2 x3 x 4 x5 least once knew) that e 1 x ... . If we replace x here with x 2 , 2 6 4! 5! 4 6 2 x x x8 x10 ... . Now, we can treat that as a polynomial we get that e x 1 x 2 2 6 24 120 and integrate it from 0 to 1. We should get an approximation that becomes more accurate as we take more terms of the expansion. Try this with different numbers of terms, say up to the last term shown in the sum. x Info – For this kind of Taylor series, people count terms in different ways. Some people would say the series given has six terms, because it has, well, six terms. Others would say it has five, because they don’t count the constant. Still others would say that it has ten, or eleven, terms but all the odd-numbered terms happen to be zero. I’ll say it’s a tenth-degree approximation, and that its integral is an eleventh degree approximation. Write it up. Nicely. Make sure you answer all the questions. Make sure you explain your procedure. I think it might be nice to include nice graphs of the three functions involved. I think they look like this: 1 2.5 0.8 2 0.6 1.5 0.4 1 0.2 0.5 0.5 Lab 6 1 1.5 2 2.5 3 -3 Integration -2 -1 1 2 3 5