SUNY Geneseo Department of Mathematics
Math 230 02
Spring 2015
Prof. Doug Baldwin
Complete by Friday, March 13
Grade by Wednesday, March 25
This lesson introduces you to two classic algorithms for evaluating definite
integrals. In the process, it also develops your ability to program with
for
loops, and reinforces your ability to work with vectors
and plotting.
Numerical integration is the process of evaluating a definite integral for its numeric value. A number of algorithms for numerical integration have been developed, starting in the days when people had to evaluate integrals by hand, and more recently intended for implementation on computers. The Wikipedia article on numerical integration provides a good sense of the variety of methods that are employed, although it covers too much material in too little space to give a very good understanding of any one method.
You will explore two classical simple numerical integration algorithms in this lesson: Riemann sums and the trapezoid rule. For an introduction to Riemann sums see the Khan Academy videos on “Simple Riemann Approximation Using Rectangles” and “Generalizing a Left Riemann Sum with Equally Spaced Rectangles.” For the trapezoid rule, see “Trapezoidal Approximation of Area under Curve.”
This lesson also introduces two new Matlab features: for
loops and
function handles. Attaway’s text discusses for
loops in
section 5.1, and function handles in sections 10.1 and 10.2. We will also talk
about both of these features in class, probably on March 9 and 11.
Riemann sums and the trapezoid rule are both algorithms for evaluating definite integrals. Any time you have multiple algorithms for solving the same problem the question of which algorithm is “best” arises. The answer usually depends on context, i.e., sometimes you need a fast solution above all else, sometimes you need one that is very accurate, sometimes you need one that fits in a small amount of computer memory, and very often you need one that strikes a balance between many criteria. In this lesson you will compare Riemann sums and the trapezoid rule to see which one most quickly converges on a close approximation to the actual value of an integral. The comparison will be qualitative rather than quantitative, and, as you will see, not entirely clear cut.
Write two Matlab functions, one of which uses Riemann sums to compute a definite integral and the other of which uses the trapezoid rule.
Both functions should take a function handle for the function being integrated, the bounds of the interval over which to integrate, and the number of sub-intervals to divide that interval into as arguments. Both functions should return the estimated integral as their result. Finally, both functions should draw a graph showing the function they are integrating and the rectangles or trapezoids that they use to approximate the area under it.
Use your functions to visually explore how Riemann sums and the trapezoid rule approximate the area under the function y = x3 between x = 0 and x = 1. Try this for a number of sub-intervals (usually called n in discussions of numerical integration) ranging from 4 or 5 up to 100 or more. Then repeat this exploration for the function y = sinx between x = 0 and x = π. Based on what you see, which integration algorithm do you think will produce the most accurate results with the fewest sub-intervals?
To test your insights from Step 1, write a Matlab script that visualizes how close the estimated integral computed by each of the functions from Step 1 is to the actual integral. This script should step through a number of n values, call the Riemann and trapezoid functions for each n, and plot the returned values against n. Your script should also plot a straight line whose Y value is the actual value of the integral being estimated by the Riemann and trapezoid functions.
Even though the instructions above say to call your Riemann and trapezoid functions for various n values, you may actually call modified versions of those functions that don’t do the plotting—you may well call the functions hundreds or thousands of times, and you probably don’t want to see that many plots. But note that I do want you to write functions that plot also, and to use those functions to visually see how the two algorithms approximate the area under a curve.
Run your script for the function y = x3 with x ranging from 0 to 1. Also run your script for y = sinx with x ranging from 0 to π. Are the visualizations consistent with what you expected from inspecting plots in Step 1? In general, would you favor Riemann sums over the trapezoid rule for computing definite integrals, or vice versa, or can you not say? What caveats come with your answer?
I will demonstrate the kinds of plots and functions I want you to write for this exercise in class.
I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring a written solution to the exercise to your meeting, as that will speed the process along.
Sign up for a meeting via Google calendar. If you worked in a group on this exercise, the whole group should schedule a single meeting with me. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.