3
$\begingroup$

This Manipulate program below allows the user to enter points on a LocatorPane and then draws a line through the points.

enter image description here

The Manipulate has an Initialization, where the programmer can define initial values for the points. The initial values are the variable ipts in the code below. What I observe is that the first time I evaluate the cell, the Manipulate works as expected. But, if I change the initial points and then evaluate the cell, the Manipulate shows the previous values. After evaluating the cells with the Manipulate definition a second time, the plots shows the revised values.

This thread seems to discuss the same issue,How to eliminate the need to double evaluate a manipulate , but applying those answers to this question isn't clear to me.

Suspect that I am misunderstanding some fundamental concept about how initialization works. Could someone point out how to revise the code and/or a discussion of what is going on here?

myTest2 = Manipulate[
   (*User points*)
   posSorted = Sort[pos];
    xMin = Min[posSorted[[All, 1]] ];
   xMax = Max[posSorted[[All, 1]] ];
   gvfuncUser = Interpolation[posSorted, InterpolationOrder -> 1];
    myPlot = Plot[gvfuncUser[x], {x, xMin, xMax}, 
     PlotRange -> {{0, 1}, {0, 1}}, Frame -> True, ImageSize -> 400];
   Grid[{
     {LocatorPane[Dynamic@pos, myPlot, LocatorAutoCreate -> True, 
       ContinuousAction -> False] }
     }]
   ,
   (*list of controls*)
   {{pos, ipts}, ControlType -> None}
   (*Initialization*)
   , TrackedSymbols :> {pos, ipts}
   , Initialization :> (
     ipts = {{0.15, 0.35}, {0.25, 0.15}, {0.50, 0.17}, {0.75,0.18}, {1, 1}};
     posSorted = {}; (*do this so that posSorted is local to this Manipulate (?) *)
     gvfuncUser = {};
     )
   , SynchronousUpdating -> False
   ];
  myTest2

In summary, to reproduce the question

  1. Evaluate the cells
  2. Change the values of ipts, for example, change the first point to {0.15, 0.05}
  3. Evaluate the cells, (the displayed points will not change to the revised values)
  4. valuate the cells, (the displayed points will change to the revised values)
$\endgroup$
1
  • $\begingroup$ As I remember, the initialization works at creation of the dynamic object. If you run the Manipulate[..] again but keeping the dynamic content alive, the initialization will not be evaluated. The reset of the data can be done by trivial addition of the ipts={...} before the Manipulate code. $\endgroup$
    – Rom38
    Oct 19, 2017 at 5:40

1 Answer 1

2
$\begingroup$

Manipulate[body, spec, opts]

While body of Manipulate waits for Initialization, it is not the case with variables spec part, at least not in the same way. The problem in judgement here is that Manipulate is a big black box and not every aspect is documented.

But here is my guess: The first time you run it it passes symbolic ipts as an initial value for pos and somwhere during creation it gets values set in Initialization.

The second time you run it, with different values, it is no longer the case because ipts already have previous values so they are used. So from now one the initial value will be the one from previous setting.

Unless you put ips = {...} inside the body for example.

Another quick fix is to abandon ipts:

Manipulate[
  ...
  {pos, ControlType -> None}
  ...
, Initialization :> (pos = {{0.1, 0.35}, ...} )
, ...
]
$\endgroup$
1
  • $\begingroup$ ok. dropping ipts will be fine. $\endgroup$
    – user6546
    Oct 20, 2017 at 5:57

Your Answer

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

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