I have:
Clear[f, a, b, n, dx];
f[x_] = x^2;
a = 0; b = 1; dx = (b - a)/n;
Manipulate[
Show[Plot[f[x], {x, a, b},
PlotStyle -> Thick,
AxesLabel -> {x, y}],
Graphics[{
Table[
{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx, 0}, {a + (i + 1) dx, f[a + i dx]}]}, {i, 0,
n - 1, 1}]
}]],
{{n, 10}, 10, 50, 10}
]
But I get an error:
Coordinate {$CellContext`n^(-1), 0} should be a pair of numbers, or a Scaled or Offset form.
Can someone tell me what I am doing wrong?
Thanks to Mahdi, I now have:
Manipulate[Module[{a = 0, b = 1, dx, f, rightSum},
dx = (b - a)/n;
f[x_] = x^2;
rightSum = N@Sum[f[a + i dx] dx, {i, 1, n}];
Show[Plot[f[x], {x, a, b},
PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{
Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx, 0}, {a + (i + 1) dx,
f[a + (i + 1) dx]}]}, {i, 0, n - 1, 1}],
Text[
"N = " <> ToString[n] <> ", R = " <>
ToString[rightSum], {(a + b)/2, f[b]}]
}]]],
{{n, 10}, 10, 50, 10, Appearance -> "Labeled"}]
Which produces this image:
Couple things I learned:
I tried:
Module[{a = 0, b = 1, dx=(b-a)/n, f, rightSum}
It worked, but when I changed the b=1 to a b=2, it did not work. So, apparently the dx=(b-a)/n does not use the b=1 in the module.
On the other hand, when I put it inside the module,
Manipulate[Module[{a = 0, b = 1, dx, f, leftSum}, dx = (b - a)/n;
it worked.
- Thus far, I have two manipulates in my notebook, which don't seem to interfere with one another or all of the static code I have in the notebook.
Difficulty with Initialization: After reading Martin John Hadley's comment, I tried the following:
Manipulate[
Show[Plot[f[x], {x, a, b},
PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{
Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx[n], 0}, {a + (i + 1) dx[n],
f[a + i dx[n]]}]}, {i, 0, n - 1}],
Text["N = " <> ToString[n] <> ", L = " <>
ToString[leftSum], {(a + b)/2, f[b]}]
}]],
{{n, 10}, 10, 50, 10, Appearance -> "Labeled"},
Initialization :> (
dx[n_] := (b - a)/n;
f[x_] = x^2;
leftSum = N@Sum[f[a + i dx[n]] dx[n], {i, 0, n - 1}];
a = 0;
b = 2
)]
But it didn't work. I got errors such as:
Coordinate {Rational[1, 5][10], 0} should be a pair of numbers, or a Scaled or Offset form.
I cannot determine what is wrong here? Any thoughts?
Answer to my Own Question: I should have put a=0 and b=2 as the first two lines in the initialization block.
Final Result due to help from Martin John Hadley:
Manipulate[
Show[Plot[f[x], {x, a, b}, PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx[n], 0}, {a + (i + 1) dx[n],
f[a + (i + 1) dx[n]]}]}, {i, 0, n - 1, 1}],
Text["N = " <> ToString[n] <> ", R = " <>
ToString[rightSum[n]], {(a + b)/2, f[b]}]}]], {{n, 10}, 10, 50,
10, Appearance -> "Labeled"},
Initialization :> (a = 0; b = 1; dx[n_] := (b - a)/n; f[x_] := x^2;
rightSum[n_] := N@Sum[f[a + i dx[n]] dx[n], {i, 1, n}])]
And the output image:
Thanks to both Martin and Mahdi for tremendous help.
Module[{a = 1, b = 1, n = 1, dx = (b - a)/n}, Print[dx]]
, you clearly see that indx = (b - a)/n
,b
,a
, andn
are blue (not green)! And the code returns(-a+b/n)
. $\endgroup$Edit>Preferences>Appearance
. $\endgroup$