2
$\begingroup$

I am preparing an activity that simulates the response from a noisy spectrometer. The function I use to generate the data has the form:

curve[a_, x_] := 
  a PDF[NormalDistribution[0, 1], x] + 
   RandomVariate[NormalDistribution[0, 0.03]];

Which produces something like:

Plot[curve[1, x], {x, -5, 5}, PlotPoints -> 200, MaxRecursion -> 2]

enter image description here

and I can get the coordinates using a right mouse button click on the plot. The right mouse button works differently in a CDF than it does in a Notebook and I would like to ultimately turn this activity into a CDF-enabled web page. My first attempt is to use a Locator

ControllerManipulate[
 Plot[curve[1, x], {x, -5, 5}, PlotPoints -> 200, MaxRecursion -> 2, 
  Epilog -> Inset[p, Scaled[{0.9, 0.9}]]], {p, Locator}]

Mathematica graphics

Which does the job except that when I move the locator, the "noise" in my plot changes. How do I modify my curve function so that I maintain the ability to generate a "unique" noise pattern each time curve is called but it does not continuously update when the end user moves a locator?

$\endgroup$

2 Answers 2

3
$\begingroup$

The issue is not with the curve function, but with your ControllerManipulate code. It is re-evaluating the entire argument (Plot[...]) every time the locator changes. You need to insert a Dynamic[] in the expression so that only that component of the expression gets updated when the locator moves. Use this:

ControllerManipulate[
    Plot[curve[1, x], {x, -5, 5}, PlotPoints -> 200, MaxRecursion -> 2, 
    Epilog -> Dynamic@Inset[p, Scaled[{0.9, 0.9}]]], {p, Locator}
]
$\endgroup$
3
$\begingroup$

Or memoize the curve:

ClearAll[curve];
curve[a_, x_] := curve[a, x] = (a PDF[NormalDistribution[0, 1], x] + 
                 RandomVariate[NormalDistribution[0, 0.03]])

ControllerManipulate[Plot[curve[1, x], {x, -5, 5}, PlotPoints -> 200, MaxRecursion -> 2, 
                     Epilog -> Inset[p, Scaled[{0.9, 0.9}]]], {p, Locator}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ In this particular activity, I do want to be able to generate the same curve with "different" noise - in a sense, simulating multiple trials of the same sample. This might work if I memorize the random seed used for each run... $\endgroup$ Commented Jan 25, 2014 at 2:59

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.