# Monte Carlo Integration with manipulate

I did a manipulate algorithm to see how Monte Carlo Integration precision works, using as example the Cauchy Function.

When Dynamic Updating is enabled, the resulting MC calculation keeps evaluating. Why?

funcCauchy[X_, A_] := Module[{x = X, a = A}, 1/(1 + (x/a)^2)];

Manipulate[
b = 2.;
a = -2.;
c = 1.5;
n = 0;
X = 0;
Y = 0;
data = Do[
X = RandomReal[{-2, 2}];
Y = RandomReal[{0, 1.5}];
fx = funcCauchy[X, 2];
If[Y < fx, n = n + 1],
Num];

TextCell[
Column[{{"resultado da Integração de f (x, a) = 1/(1 + (x/a)^2) é  \
= ", Integrate[
1/(1 + (x/2.)^2), {x, -2,
2}]}, {"resultado da Integração por Monte Carlo, com N= ", Num,
" é igual a = ",
N[Integ = (b - a)*c *(n/Num), {Infinity, 6}]}}], FontSize -> 12,
TextAlignment -> Left, CellFrame -> True,
CellMargins -> {{300, 300}, {20, 20}}],
{{Num, 10, "N = N\[Degree]. de Rodadas"}, Appearance -> "Open", 10,
10^5, 100}, ContinuousAction -> False, ContentSize -> {700, 100}

]


Please, be free to improve this manipulate algorithm settings!

• Have you tried TrackedSymbols? – lowriniak Dec 1 '16 at 12:03
• Did not work, TrackedSymbols :> {num} – locometro Dec 1 '16 at 12:08
• Well first you need to make sure the symbol you are tracking is exactly the same as the one you are using (so Num rather than num). You will also want to consider if any other symbols should trigger an update. – lowriniak Dec 1 '16 at 12:11
• Oh, Thanks! It worked, TrackedSymbols :> {Num}. :) – locometro Dec 1 '16 at 12:18
• @lowriniak Make it a full answer and I will upvote it. Didn't know about this option and find it quite useful, too :-) – pbx Dec 1 '16 at 15:49

Expanding on my comment, if you have a Manipulate that continually refreshes or has poor performance issues, you will need to do some evaluation control. In this case the option you need is TrackedSymbols which is given to the whole Manipulate. In this case you would write:

funcCauchy[X_, A_] := Module[{x = X, a = A}, 1/(1 + (x/a)^2)];

Manipulate[
(*These are currently global variables so you also may want to add LocalizeVariables -> True as well*)
b = 2.; a = -2.; c = 1.5; n = 0; X = 0; Y = 0;
data = Do[
X = RandomReal[{-2, 2}];
Y = RandomReal[{0, 1.5}];
fx = funcCauchy[X, 2];
If[Y < fx, n = n + 1],
Num
];

TextCell[
Column[{{"resultado da Integração de f (x, a) = 1/(1 + (x/a)^2) é  \
= ", Integrate[
1/(1 + (x/2.)^2), {x, -2,
2}]}, {"resultado da Integração por Monte Carlo, com N= ", Num,
" é igual a = ",
N[Integ = (b - a)*c *(n/Num), {Infinity, 6}]}}], FontSize -> 12,
TextAlignment -> Left, CellFrame -> True,
CellMargins -> {{300, 300}, {20, 20}}],

(*The only control variable is Num*)
(*Therefore, assuming this is a standalone tool, you only want Num to be tracked for changes*)
{{Num, 10, "N = N\[Degree]. de Rodadas"}, Appearance -> "Open", 10,
10^5, 100},
ContinuousAction -> False,
ContentSize -> {700, 100},

(*TrackedSymbols takes a list of symbols and only updates the Manipulate when one of them changes*)
(*remember to use RuleDelayed so they are treated as symbols and not their values*)
TrackedSymbols :> {Num}
]