2
$\begingroup$

Let's say I have a function defined as a module where at the end I call up "Manipulate" as below:

a[x_]:= Module[{k},k = 2 x; Manipulate[Something[y], {y, 0, k}]

What does Mathematica complain about the variablek? It says "a variable was used where it is probably not going to be evaluated before going out of scope"

Is there any way I can fix this and achieve what I want?

$\endgroup$
1
  • 1
    $\begingroup$ Could you share minimal but complete example which reproduces the message? Take a look at notes of JF in 29461. Also, Manipulate is HoldAll and usually With should be used to 'inject' variables inside holding expressions. See closely related: 72754 and guidelines: 559 $\endgroup$ Commented Aug 5, 2017 at 21:33

1 Answer 1

2
$\begingroup$

Don't use Module in the situation you describe. Module is a lexical scoping construct. The k give to Module as its 1st argument is a statically scoped variable. Your Manipulate expression builds its own dynamically scoping regime, so the k mentioned in your specification of the manipulator control y is in a different scope and is a different variable. Look at your code wrapped in FullForm to see that this is so.

The fix is to use DynamicModule in place of Module.

a[x_] :=
  DynamicModule[{k},
    k = 2 x;
    Manipulate[
      1/y^k,
      {y, .1, k, Appearance -> "Labeled"}]]

a[.5]

demo

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.