Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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}
]
share|improve this answer

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

share|improve this answer
    
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... –  bobthechemist Jan 25 at 2:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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