How to put the sum inside the manipulate [duplicate]

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}
]

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

• @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. – Sjoerd C. de Vries Nov 11 '15 at 7:59
• Please don't vandalize your own post. If you want to disassociate this post from your account, please follow this post or this. – Andrew T. Nov 11 '15 at 8:49

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}] From here you can position things and set font styles and sizes etc

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}] • +5 if I am allowed! – thils Nov 11 '15 at 9:26
• 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 :-) – Jason B. Nov 11 '15 at 9:28
• +1 legal contribution made. Dead right about feeding off imaginary internet points.....still better than "h index" for ghost authors – thils Nov 11 '15 at 9:42

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 • 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. – Juan Valdiri Nov 11 '15 at 5:47
• Sure, first answer is the best way then. – thils Nov 11 '15 at 6:05