# 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 '16 at 3:54

## 1 Answer

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];
Which[menu == "fright", Show[{fright, curve1}], menu == "fleft",
Show[{fleft, curve1}], menu == "fmid", Show[{fmid, curve1}]],
{menu, {"fright", "fleft", "fmid"}}, {{squarenum, 4}, 2, 20}]

• Thank you, I'm a step closer with that. I just need to figure out how to manipulate the number of rectangles so that they can increase/decrease based on the user's desire – Mercedes Rodriguez Oct 17 '16 at 1:57
• Make a function that has number of rectanges as an input and then assign the manipulate to change that number. – bill s Oct 17 '16 at 2:07
• Do you have any guidance on how I could do that? I tried manipulating the delta variable since it contains "squarenum" (number of rectangles) but it wouldn't alter anything – Mercedes Rodriguez Oct 17 '16 at 2:13
• I've added a second slider to handle the number of rectangles, – bill s Oct 17 '16 at 2:19
• You're a lifesaver, this helped like crazy. – Mercedes Rodriguez Oct 17 '16 at 2:28