2
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Moo
    Commented Oct 17, 2016 at 3:54

1 Answer 1

1
$\begingroup$

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}]
$\endgroup$
6
  • $\begingroup$ 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 $\endgroup$ Commented Oct 17, 2016 at 1:57
  • $\begingroup$ Make a function that has number of rectanges as an input and then assign the manipulate to change that number. $\endgroup$
    – bill s
    Commented Oct 17, 2016 at 2:07
  • $\begingroup$ 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 $\endgroup$ Commented Oct 17, 2016 at 2:13
  • $\begingroup$ I've added a second slider to handle the number of rectangles, $\endgroup$
    – bill s
    Commented Oct 17, 2016 at 2:19
  • $\begingroup$ You're a lifesaver, this helped like crazy. $\endgroup$ Commented Oct 17, 2016 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.