MATLAB program- homework
Lab 10: Riemann Sums
CS 122 • 15 Points Total
Objectives
· Use Riemann Sums to approximate integrals
· Practice functions, conditions, loops
Overview
In mathematics, the definite integral of a function f on the interval [a, b] can be written as:
, and can be visualized as the total area bounded by the function on that interval.
For example, if we look at the function between x = 1 and x = 4:
Then the integral for the function on that interval is the area of the enclosed region, between the function and the x axis. Anything below the axis (if applicable) is subtracted from the region above the axis.
There are two ways to approximate the area under a function without calculating an integral. The first is a Riemann sum and the second is a Trapezoidal sum. A Riemann Sum is calculated by fitting a series of rectangles to the function curve and then summing the area of those rectangles. It follows that the more rectangles used, the smaller they are, which makes for a more accurate approximation.
Calculating a Riemann Sum:
Each rectangle has a width of .Their height is found by evaluating the function f at some point
, where:
,
with i = 0 : n;
and
For the purposes of this Lab, we are assuming that all of the rectangles are of equal width. We can see that the first and last x values are the boundary points for the interval; a and b.
Types of Riemann Sum
There are three types of Riemann Sums; left, right, middle. The Trapezoidal Sum is sometimes included as a subtype of Riemann Sums, but here we will address it separately.
Left Riemann Sum
For a left Riemann Sum, use the left edge of each segment to find the height of the rectangles.
This plot shows an example of a Left Riemann Sum, for n = 10. Notice how the top left corner of each rectangle intersects the curve.
Left Riemann Sums tend to underestimate functions that mostly increase, and overestimate functions that are generally decreasing.
Right Riemann Sum
For a right Riemann Sum, use the right edge of each segment to find the height of the rectangles.
This plot shows an example of a Right Riemann Sum, for n = 10. Notice how the top right corner of each rectangle intersects the curve.
Right Riemann Sums tend to overestimate functions that mostly increase, and underestimate functions that are generally decreasing.
Middle Riemann Sum
For a Middle Riemann Sum, use the midpoint of each segment to find the height of the rectangles.
This plot shows an example of a Middle Riemann Sum, for n = 10. Notice how the center of the top of each rectangle intersects the curve.
Trapezoidal Rule
The Trapezoidal Rule is another method for approximating a definite integral. Instead of approximating the area under a curve by a series of rectangles, the Trapezoidal Rule uses a series of trapezoids. It tends to provide a more accurate approximation than any of the preceding.
The provided plot shows a similar curve, where n = 11.
Implementation
Download the Lab10 folder from Bblearn. Move it to your cs122 directory, cd into that directory and add it to the path.
For this lab you will be writing four functions, one for each of the discussed methods. Each will take a function f, a lower bound a, an upper bound b, and a value for n. The result for each should be a single value representing the approximation.
When you have finished, open the Lab10.m file and follow the instructions in the comments. You will need to ask the user to input various things, pass the input to the four functions you previously implemented (this has been done for you), display the results, and graph the function.
The function you will be approximating is:
f(x) = 2x4 – 5x2 – 10x + 9, 0<= x <= 9
Using each of your functions, calculate an approximation of the integral on the given interval for n = 10, 20 and 40. Use the built-in MATLAB function rsums() to check your work.
Discuss any error, and comment on how the approximations change: both with respect to each other for a value of n, and as n increases.
Deliverables
· Your project report (see below)
· Your zipped lab 10 folder, containing Lab10.m, Left_Riemann.m, Right_Riemann.m, Middle_Riemann.m and Trapezoidal_Method.m
· If you are not sure how to zip a folder, please ask your TA for assistance
Project Report
Below is the point distribution for required sections in the lab report. Be sure that each
section is labeled clearly. Refer to the lab submission guidelines for details on what
goes in each section.
|
Section |
Points |
Notes |
|
1. Task Description |
1 |
|
|
2.Learning Objectives |
1 |
|
|
3.Approach |
1 |
|
|
4.Mathematical Concepts |
1 |
|
|
5.Program Inputs |
0.5 |
|
|
6.Program Outputs |
0.5 |
|
|
7.Program Description |
1 |
|
|
8.Source Code |
5 |
|
|
9.Code Execution Results |
3 |
· Show the calculated result of each of your integral approximation functions for each of the indicated values of n. · Discuss how your results compare to the result of calling the rsums function. · Discuss how your approximation values differ from each other for a given value of n. · Discuss how your approximation values change as n increases. · Show the plot of the mathematical function. Label the axes and provide a title. |
|
10.Conclusions |
1 |
|
|
|
15 total |
|
References:
http://en.wikipedia.org/wiki/Riemann_Sums (for the images)