# Using Manipulate and Module for Riemann Sum

I'm trying to write a program using the Manipulate function that shows the leftmost, rightmost, and midpoint Riemann sums for a function. I've successfully coded the graphs for each one, however I'm not sure how to apply the Manipulate function to it. Here are the graphs I've coded:

f[x_] := 3 x^2 + 5
rectangles =
Module[{low, high, squarenum, delta, fright, fleft, fmid, curve1},
low = 0; high = 2;
squarenum = 4;
delta = (high - low)/squarenum;
fright =
Graphics[{FaceForm[{Opacity[0.7]}],
Table[{Hue[0.79, 0.92, 0.88], EdgeForm[Thin],
Rectangle[{low + (n - 1)*delta, 0}, {low + n*delta,
f[low + n*delta]}]}, {n, 1, squarenum} ]}, ImageSize -> 50];
fleft = Graphics[{FaceForm[{Opacity[0.7]}],
Table[{Hue[0.79, 0.92, 0.88], EdgeForm[Thin],
Rectangle[{low + n*delta, 0}, {low + (n + 1)*delta,
f[low + n*delta]}]}, {n, 1, squarenum} ]}, ImageSize -> 62];
fmid = Graphics[{FaceForm[{Opacity[0.7]}],
Table[{Hue[0.79, 0.92, 0.88], EdgeForm[Thin],
Rectangle[{low + (n - 0.5)*delta,
0}, {(low) + (n + 0.5)*(delta), f[low + n*delta]}]}, {n, 1,
squarenum} ]}, ImageSize -> 56];
curve1 =
Plot[f[x], {x, low, high}, PlotStyle -> Thick, ImageSize -> 200];
Show[{fright, curve1}] Show[{fleft, curve1}] Show[{fmid, curve1}]]


Any help would be appreciated, I've been racking my brain and have been stuck for hours

• It would be really slick to add these controls: mathworld.wolfram.com/RiemannSum.html, including the estimated and actual integral result - very adaptive little applet with all the methods from drop-downs. The code is on that site, but needs work.
– Moo
Oct 17, 2016 at 3:54

Here's one way that just plots the specified graph and lets you change the variable squarenum.

f[x_] := 3 x^2 + 5
plot[squarenum_] := Module[{}, low = 0; high = 2;
delta = (high - low)/squarenum;
fright =
Graphics[{FaceForm[{Opacity[0.7]}],
Table[{Hue[0.79, 0.92, 0.88], EdgeForm[Thin],
Rectangle[{low + (n - 1)*delta, 0}, {low + n*delta,
f[low + n*delta]}]}, {n, 1, squarenum}]}, ImageSize -> 50];
fleft =
Graphics[{FaceForm[{Opacity[0.7]}],
Table[{Hue[0.79, 0.92, 0.88], EdgeForm[Thin],
Rectangle[{low + n*delta, 0}, {low + (n + 1)*delta,
f[low + n*delta]}]}, {n, 1, squarenum}]}, ImageSize -> 62];
fmid =
Graphics[{FaceForm[{Opacity[0.7]}],
Table[{Hue[0.79, 0.92, 0.88], EdgeForm[Thin],
Rectangle[{low + (n - 0.5)*delta,
0}, {(low) + (n + 0.5)*(delta), f[low + n*delta]}]}, {n, 1,
squarenum}]}, ImageSize -> 56];
curve1 = Plot[f[x], {x, low, high}, PlotStyle -> Thick, ImageSize -> 200];];

Manipulate[plot[squarenum];