1
$\begingroup$

This question already has an answer here:

Manipulate[
  a = 0;
  b = 10;
  dx = (b - a)/n;
  f[x_] = 2*x^3 + 5*x^2 + 3*x + 2;

  Show[
    DiscretePlot[
      2*x^3 + 5*x^2 + 3*x + 2,{x, 0, b, dx},
      ExtentSize -> Left,
      PlotRange->{{a, 10.5},{0, 2600}},
      ColorFunction -> (Green &),
      ExtentElementFunction -> 
        ({EdgeForm[Black], Rectangle @@ Transpose @ #} &)
    ],
    Plot[
      2*x^3 + 5*x^2 + 3*x + 2, {x, a,b }, 
      PlotStyle -> {Black, Thick}]
    ],
    Sum[dx*f[a + i*dx],{i, 1, n},{n, 5, 50}], {n, 5, 50}
]
$\endgroup$

marked as duplicate by MarcoB, user9660, dr.blochwave, Oleksandr R., Öskå Nov 11 '15 at 10:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @marcoB No, given the title, OP is looking to add the numerical value to the plot, he's not asking about the Riemann sum itself. $\endgroup$ – Sjoerd C. de Vries Nov 11 '15 at 7:59
  • $\begingroup$ Please don't vandalize your own post. If you want to disassociate this post from your account, please follow this post or this. $\endgroup$ – Andrew T. Nov 11 '15 at 8:49
7
$\begingroup$

The problem is that you haven't got this of the form Manipulate[expr,{x,xmin,xmax}]. Using Column is one way you can group everything and then adjust the layout with e.g. Alignment option. Grid and Row are other options you could consider.

Manipulate[a = 0; b = 10; dx = (b - a)/n;
 Column[{Show[
    DiscretePlot[2*x^3 + 5*x^2 + 3*x + 2, {x, 0, b, dx}, 
     ExtentSize -> Left, PlotRange -> {{a, 10.5}, {0, 2600}}, 
     ImageSize -> 400, ColorFunction -> (Green &), 
     ExtentElementFunction -> ({EdgeForm[Black], 
         Rectangle @@ Transpose@#} &)], 
    Plot[2*x^3 + 5*x^2 + 3*x + 2, {x, a, b}, 
     PlotStyle -> {Black, Thick}]], 
   N@Sum[dx*f[a + i*dx], {i, 1, n}, {n, 5, 50}]}], {n, 5, 50, 1}, 
 Initialization :> {f[x_] = 2*x^3 + 5*x^2 + 3*x + 2}]

enter image description here

From here you can position things and set font styles and sizes etc

$\endgroup$
6
$\begingroup$

Your sum is all wrong, you aren't getting the right answer or even close. You can check by taking the actual integral.

Anyway, you can use Inset to put any text inside your graphic, like this

Manipulate[a = 0; b = 10; dx = (b - a)/n;
 xi[i_] := Which[Method == "Upper", a + (i + 1)*dx,
   Method == "Lower", a + i*dx,
   Method == "Middle", a + dx/2 + dx i];
 rectangles = 
  Table[{Opacity[0.3], Green, EdgeForm[Gray], 
    Rectangle[{a + i*dx, 0}, {a + (i + 1)*dx, f[xi[i]]}]}, {i, 0, 
    n - 1, 1}];
 Show[Plot[f[x], {x, a, b}, 
   PlotStyle -> {Black, Thick}],
  Graphics@rectangles, 
  Epilog -> {Inset[
     "\!\(\*SubscriptBox[\(\[Sum]\), \
\(i\)]\)f(\!\(\*SubscriptBox[\(x\), \(i\)]\))dx = " <> 
      ToString[N@Sum[dx*f[xi[i]], {i, 0, n - 1}]], Scaled[{.3, .8}]], 
    Inset["\[Integral]f(x)\[DifferentialD]x = " <> 
      ToString[NIntegrate[f[x], {x, 0, 10}]], Scaled[{.3, .6}]]}], {{n,5},
   5, 50, 1, 
  Appearance -> "Open"}, {{Method, "Upper"}, {"Upper", "Lower", 
   "Middle"}, ControlType -> Setter}, 
    Initialization :> {f[x_] = 2*x^3 + 5*x^2 + 3*x + 2}]

enter image description here

$\endgroup$
  • $\begingroup$ +5 if I am allowed! $\endgroup$ – thils Nov 11 '15 at 9:26
  • $\begingroup$ You are allowed to show appreciation, easiest way is to click the up facing triangle, which gives reputation points. And in this day and age, we all feed off imaginary internet points :-) $\endgroup$ – Jason B. Nov 11 '15 at 9:28
  • 1
    $\begingroup$ +1 legal contribution made. Dead right about feeding off imaginary internet points.....still better than "h index" for ghost authors $\endgroup$ – thils Nov 11 '15 at 9:42
4
$\begingroup$

A possible option

 Manipulate[a = 0; b = 10; dx = (b - a)/n; 
     f[x_] = 2*x^3 + 5*x^2 + 3*x + 2; 
        Show[DiscretePlot[2*x^3 + 5*x^2 + 3*x + 2, {x, 0, b, dx}, 
       ExtentSize -> Left, 
            PlotRange -> {{a, 10.5}, {0, 2600}}, 
       ColorFunction -> (Green & ), 
            ExtentElementFunction -> ({EdgeForm[Black], 
           Rectangle @@ Transpose[#1]} & )], 
          Plot[2*x^3 + 5*x^2 + 3*x + 2, {x, a, b}, 
       PlotStyle -> {Black, Thick}]], {n, {6, 7, 8, 9, 10}}]

Note: I have deleted the last code line

enter image description here

$\endgroup$
  • $\begingroup$ Thank you for answering my question but i really want is that I can put the sum of Riemman into the code, so that when I get more a bigger interval it will get a more accurate result to the integral value. $\endgroup$ – Juan Valdiri Nov 11 '15 at 5:47
  • $\begingroup$ Sure, first answer is the best way then. $\endgroup$ – thils Nov 11 '15 at 6:05

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