4
$\begingroup$

I get unexpected results when I run the following code (even before moving the sliders):

  InfPoint[ang_] := If[ang <= 1 , 5, 0];
  Manipulate[
    Print[0,    "\t", InfPoint[0]];
    Print[ang, "\t", InfPoint[ang]],
    {x, 0, 2},
    {ang, x, 2}
  ]
0 5
0   If[0<=1,5,0]

Notice that calling InfPoint with an argument fails to evaluate, even though the argument seems to be passed correctly. When I change the last "x" in the code to a "0", everything works as expected.

This code is modeled after this example found at tutorial/AdvancedManipulateFunctionality, which seems to work just fine:

Manipulate[
  Row[{
    "(",
    Column[{n,m},Center],
    ")=",
    Binomial[n,m]  
  }],
  {n,1,10,1},
  {m,1,n,1}
]

There's something going on here that I don't understand. Can anyone explain to me how I should think about these nested controls?

(I am running Mathematica 10.0.2.0.)

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 May 10 '15 at 21:09
7
$\begingroup$

One difficulty in using Manipulate is that it rewrites your code for you in ways that are not clearly explained in the documentation. The thinking is that this rewriting achieves the dynamic interactions described in the documentation without burdening the programmer with some of the tedious details involved. For instance, expressions that contain Manipulate variables get wrapped in Dynamic, so that they will be updated when the variable changes. This allows controls to interact with each other. But exactly how and when code is rewritten is not clearly explained.

The documentation somewhat vaguely suggests that a variable/control declaration of the form

{var, a, b}

should be equivalent to

{{var, a}, a, b}

For instance, from tutorial/IntroductionToManipulate:

It would be convenient to set their initial value to something other than the default left-most value. This is done by using a variable specification of the form {{var, init}, min, max}.

In the OP's case, the declaration

{ang, x, 2}

should be written

({ang, x}, Dynamic[x], 2}

and result in a control

Manipulator[Dynamic[ang], {Dynamic[x], 2}]

placed in the Manipulate output when shown in the Front End, with ang initialized to x. This is what m_goldberg and Kuba were referring to in their answer and comment respectively.

In fact what seems to happen is that the rewrite results in

{ang, Dynamic[x], 2}

which is equivalent, by the default initialization rule, to

{{ang, Dynamic[x]}, Dynamic[x], 2}

which also explains what Kuba observed.

In my opinion, this behavior is avoidable and should be considered a bug. However, WRI developers might disagree with me.

Note that we do not run into this problem with the example from the tutorial cited by the OP. The declaration

{m, 1, n, 1}

is rewritten

{m, 1, Dynamic[n], 1}

-- is equivalent to

{{m, 1}, 1, Dynamic[n], 1}

-- and results in the control

Manipulator[Dynamic[m], {1, Dynamic[n], 1}]

in the output with m initialized to 1. Since it is not initialized to Dynamic[n], we do not see the same problem as in the OP's code.

A workaround, as m_goldberg already pointed out, is to initialize ang explicitly.

$\endgroup$
4
$\begingroup$

You need to do some initialization. Manipulate often behaves strangely when its controls are not initialized properly.

f[u_] := If[u <= 1, 5, 0];

Manipulate[
  Column[{
    Row[{"x: ", x, "    f[x]: ", f[x]}],
    Row[{"ang: ", ang, "  f[ang]: ", f[ang]}]}],
  {x, 0, 2},
  {{ang, x}, x, 2}]  (* initialization added to control *)

demo

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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