The Trapezoid Rule
By Sylvain Muise
<smuise@student.math.uwaterloo.ca>
Introduction
The trapezoid rule is one way of many to approximate definite integrals. It is found by finding the average between the left-point rules and right-point rules defined as follows:
Left-point rule: =
Right-point rule: =
where and .
It is called the trapezoid rule because of the shape of polygons which results. This Maple application demonstrates the trapezoid rule using graphs and animations.
Code
> restart:
> leftGraph := proc(f, a, b, n) local dX, boxes, i, x0, x1, graph: dX := (b-a)/n: boxes := []: for i from 1 to n do x0 := a+(i-1) * dX: x1 := a + i * dX: boxes := [op(boxes), plottools[polygon]([[x0, 0], [x0,f(x0)],[x1,f(x0)],[x1,0]], color=cyan)]: end do: graph := plot(f(x),x=a..b): plots[display]([graph,seq(boxes[i],i=1..n)], axes=normal) end proc:
> rightGraph := proc(f, a, b, n) local dX, boxes, i, x0, x1, graph: dX := (b-a)/n: boxes := []: for i from 1 to n do x0 := a+(i-1) * dX: x1 := a + i * dX: boxes := [op(boxes), plottools[polygon]([[x0,0],[x0,f(x1)], [x1, f(x1)], [x1, 0]], color=cyan)]: end do: graph := plot(f(x), x=a..b): plots[display]([graph, seq(boxes[i],i=1..n)], axes=normal) end proc:
> trapGraph := proc(f, a, b, n) local dX, traps, i, x0, x1, graph: dX := (b - a) /n: traps := []: for i from 1 to n do x0 := a + (i-1) * dX: x1 := a + i * dX: traps := [op(traps), plottools[polygon]([[x0,0],[x0,f(x0)],[x1, f(x1)], [x1, 0]], color=cyan)]: end do: graph := plot(f(x),x=a..b): plots[display]([graph, seq(traps[i], i=1..n)], axes=normal) end proc:
> animateTraps := proc(f, a, b, initN, finalN) local i, animations, temp: if initN <= finalN then for i from initN to finalN do animations[i] := trapGraph(f,a, b, i): end do: temp := initN: temp2 := finalN: elif initN > finalN then temp := initN: for i from finalN to initN do animations[temp] := trapGraph(f,a,b,i): temp := temp - 1: end do: temp := finalN: temp2 := initN: end if: plots[display](seq(animations[i],i=temp..temp2),insequence=true, axes=normal) end proc:
Warning, `temp2` is implicitly declared local to procedure `animateTraps`
Examples
First, define a function. Ex: f := x -> sin(x); The first three functions, leftGraph, rightGraph, and trapGraph, display an image of the function and the corresponding approximation. Parameters: ????Graph(f, a, b, n) f : the function a: the lower limit of integration/approximation b: the upper limit of integration/approximation n: the number of trapezoids to use to approximate the integral The second function, animateTraps, displays an animation of the function and the trapezoidal approximation with changing values of n. Parameters: animateTraps(f, a, b, initN, finalN) f: the function a: the lower limit of integration/approximation b: the upper limit of integration/approximation initN: the initial number of trapezoids to use to approximate the integral finalN : the final number of trapezoids to use to approximate the integral Examples:
> f := x -> sin(x);
> leftGraph(f, 0, 4*Pi, 20);
> rightGraph(f, 0, 4*Pi, 20);
> trapGraph(f, 0, 4*Pi, 20);
> animateTraps(f, 0, 4*Pi, 1, 20);
> animateTraps(f, 0, 4*Pi, 20, 1);
> g := x -> x^3:
> leftGraph(g, -10, 10, 5);
> rightGraph(g, -10, 10, 5);
> trapGraph(g, -10, 10, 5);
> animateTraps(g, -10, 10, 1, 20);
> h:= x -> exp(-x^2):
> trapGraph(h, -2, 2, 5);
> animateTraps(h, -2,2,1, 30);
>