|
||||
| |
||||
| |
||||
|
This is an exercise in the use of arrays, loops, fencepost errors, and numerics. You are to write a class that computes the integral of a function (if the function is always postive, then this is the area under the curve) using the Trapezoid Rule. This method is similar to the use of rectangles (see the notes and the sample program from elsewhere on the website) but instead sums up the areas of trapezoids instead of rectangles. Where the use of rectangles uses only the left hand (or the right hand) endpoint of a series of panels, the trapezoid method uses both the left and the right hand endpoints of a panel. See the notes for details. You should use the main class found here. As with most numerical methods, there are several ways to run the program. Clearly, as the number of panels increases, the approximation should get better and better because the trapezoidal (that is, straight line) approximation to the function should be closer and closer to being correct. On the other hand, at some point one will be using so many panels that the numerical roundoff error might become significant. There are, therefore, two basic approaches to doing something like this. One could try to run the program using more and more panels until the difference between successive approximations fell below some bound epsilon. Or one could simply run the program with more and more panels, look at the results, and make a judgement as to which value to use. If, for example, one is finding the area under a function known to be always positive and known to be always increasing, then the approximation should always be increasing, because the straight line of the trapezoid will always lie below the function. However, numerical roundoff is almost always toward zero, so at some point the approximation will stop increasing and start actually to get smaller. At this point one can assume that the answer is getting less accurate, and can stop. Note that this main program asks you first to create a data array inside the class that will handle the integration, then to echo the data array, then to compute the approximation to the integral. Since you will create a data array inside the loop that increases the number of panels, you will have to allocate the data array every time you call the method to create data, because the length of the array will increase with each call. You will certainly want to echo the entire array only for a small number of data points and comment out the line that invokes the echo method once you feel your code is correct. (See hints on testing below.) You should use, and adapt as necessary, the MyFunctions class found here. This is a good template. You will want to add a line or two to use a different function. I do not object if you leave all the functional evaluation lines in place and have the method simply return the most recent one computed. You may wish instead to comment out all those but the one you actually use. Sample output for testing purposes can be found here. This is output for the function 3.0 x^2 + 1.
WARNING: You need to be very careful with the fencepost error here. If there are to be n panels, then you will need n+1 data points, but you must be careful not to include the panel whose left endpoint is the last data point in your array. If you do that, you will get an execution error in trying to access what would be the right endpoint of that panel, because that's beyond the length of the array. TESTING YOUR CODE: You should look at the sample output and use this for testing; knowning the right answer can be very valuable in doing numerical code. The actual integration is done by summing up areas of panels. I highly recommend that you echo the data points for a small number of panels (like 10, as indicated in the sample code) and that you print out a line from inside the loop doing the summation. You should perhaps print the left endpoint of the panel, the right endpoint of the panel, the functional values at the left and right endpoints, the computed area of the trapezoid for that panel, and the current running sum that is the eventual integral. If you do this for a small number of panels, like 10, then you can do the computation by hand to check that you're getting the right numbers in each iteration of the loop. The other thing to think about is that the trapezoid method approximates the function with a straight line. If you use as your function a straight line, then you should always get exactly the right answer! Instead of the quadratic of the sample output, if you use a function like y = 3.0 x + 2, then you can compute the area exactly by hand and your program should get the same answer. Knowing ground truth is always useful when debugging. This is especially valuable to make sure you are counting exactly the right number of panels and are not using one too many or one too few. THE FINAL SUBMISSION: For the final submission, you should use one million panels as your MAXSEGMENTS. At this point you will want to have the echo invocation commented out. For your final submission, use the function y = x^2 + 3.0 x + 4.0, with left endpoint 114.0 and right endpoint 120.0, for which the correct approximate areas should be
panels area
100 84282.003600
1000 84282.000036
10000 84282.000000
100000 84282.000000
1000000 84282.000000
|
|
|||



