Overview

An approximation of the area under a curve by dividing it into simple shapes, and summing those composite areas .

where , the width of each shape, usually rectangles.

Examples

Left Riemann sum

Divide the area into equal-width rectangles, where the height of each rectangle matches the function value at the left endpoint of its base.

Right Riemann sum

The height of each rectangle is equal to the value of the function at the right endpoint of its base.

Midpoint Riemann sum

Divide the area is equal to the value of the function at the midpoint of its base

Trapezoidal Rule

Use equal-height trapezoids, each touches the curve at both of its top vertices

Example

Sage Code

# Sage
var('x, i')
def left_sum(func, low_bound, upper_bound, num_recs):
    w = (upper_bound - low_bound) / num_recs
    left_sum = sum(w * func(x=low_bound + i*w), i, 0, num_recs-1)
    return left_sum
 
def mid_sum(func, low_bound, upper_bound, num_recs):
    w = (upper_bound - low_bound) / num_recs
    mid_sum = sum(w * func(x=low_bound + (i+1/2)*w), i, 0, num_recs-1)
    return mid_sum
 
def right_sum(func, low_bound, upper_bound, num_recs):
    w = (upper_bound - low_bound) / num_recs
    right_sum = sum(w * func(x=low_bound + i*w), i, 1, num_recs)
    return right_sum
 
def trapezoid_sum(func, low_bound, upper_bound, num_recs):
    w = (upper_bound - lowlow_bound + (i+1)*w))/2, i, 0, num_recs-1)
    return trap_sum