I would like to do a small demonstration of Monte Carlo integration. However, When I generated the plot code, it does not work properly.
mcPlot[nn_] :=
Module[{x, y, n = nn, colorFunction},
x = RandomReal[{0, π/2}, n]; y = N[Sin[x]];
colorFunction = ColorData["BrightBands"];
GraphicsGrid[{{Show[
Plot[Sin[x], {x, 0, π/2}, Frame -> True, ImageSize -> Large,
PlotStyle -> Thickness[0.01],
FrameLabel -> {{Text[
Style["Sin[x]", Medium, Bold, RGBColor[0.2, 0.5, 0.9]]],
None}, {Text[
Style["x ∈ [0, \!\(\*FractionBox[\(π\), \
\(2\)]\)]", Medium, Bold, RGBColor[0.2, 0.5, 0.9]]], None}}],
Graphics[{EdgeForm[
Opacity[.5]], {colorFunction[#[[1]]/(π/2)], Opacity[.05],
Rectangle[{0, 0}, {#[[1]], #[[2]]}],
colorFunction[#[[1]]/(π/2)], Opacity[.05],
Rectangle[{#[[1]], #[[2]]}, {π/2, 0}]} & /@
Transpose[{x, y}], {PointSize[0.015],
Point[Transpose[{x, y}]]}}]],
GraphicsGrid[{{Text[
Style["\!\(\*FractionBox[\(1\), \
\(n\)]\)\!\(\*UnderoverscriptBox[\(∑\), \(i = 1\), \
\(n\)]\)Sin[\!\(\*SubscriptBox[\(x\), \(i\)]\)] = ", Medium, Bold,
RGBColor[0.2, 0.2, 0.2]]],
Text[Style[Mean[y], Medium, Bold,
RGBColor[0.2, 0.2, 0.2]]]}, {Text[
Style["\!\(\*FractionBox[\(\(|\)\(S\\\ - \*SubscriptBox[\(S\
\), \(0\)]\)\(|\)\), SubscriptBox[\(S\), \(0\)]]\) = ", Medium, Bold,
RGBColor[0.2, 0.2, 0.2]]],
Text[Style[Abs[Mean[y] π/2 - 1], Medium, Bold,
RGBColor[0.2, 0.2, 0.2]]]}}]}}]]
Manipulate[mcPlot[n], {n, 1, 10}]
1.It seems the problem is due to Random and text display.
2.The text on the right does not show up nicely and it is difficult to make them together in one frame.
Can any one help me with the issue.