0
$\begingroup$

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!

$\endgroup$
9
  • 2
    $\begingroup$ Have you tried TrackedSymbols? $\endgroup$
    – lowriniak
    Commented Dec 1, 2016 at 12:03
  • $\begingroup$ Did not work, TrackedSymbols :> {num} $\endgroup$
    – locometro
    Commented Dec 1, 2016 at 12:08
  • $\begingroup$ 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. $\endgroup$
    – lowriniak
    Commented Dec 1, 2016 at 12:11
  • $\begingroup$ Oh, Thanks! It worked, TrackedSymbols :> {Num}. :) $\endgroup$
    – locometro
    Commented Dec 1, 2016 at 12:18
  • 1
    $\begingroup$ @lowriniak Make it a full answer and I will upvote it. Didn't know about this option and find it quite useful, too :-) $\endgroup$
    – pbx
    Commented Dec 1, 2016 at 15:49

1 Answer 1

6
$\begingroup$

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}
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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