# Variable scoping in Manipulate [duplicate]

Here is my attempt to manipulate the value of q for the PDF function. Whenever I use the manipulate function, nothing happens to the output. Can anyone explain why?

You have many code errors, such as the error in defining a function G[x_]= instead of G[x_]:=, the fact that sigma cannot have a value (because q is not assigned), missing semicolons, and much more.

I presume you're lifting my code from here.

Manipulate[
Integrate[
PDF[
NormalDistribution[0, -q Log[2, q] - (1 - q) Log[2, 1 - q]], x],
{x, 0, 1}],
{q, .1, .3}]

• Yes, I was the one who asked the previous question as well.
– sde7
Sep 15, 2018 at 20:28
• ...and never accepted my previous answer. Sep 15, 2018 at 20:32
• I just recently made an account.
– sde7
Sep 15, 2018 at 20:36

You have a variable scoping problem. The global q that appears in your definition of sigma is not the same as the local q that appears in your Manipulate expression. One way to fix it is:

sigma[q_] := -q Log2[q] - (1 - q) Log2[1 - q]
g[q_][x_] := PDF[NormalDistribution[0, sigma[q]], x]
Manipulate[g[q][x], {q, .0001, 1}]


Manipulate doesn't "see" the q term in the argument.

To fix this use

Manipulate[PDF[NormalDistribution[0, sigma], x] /. q -> Q, {Q, 0, 1}]


Or make sigma a function of q as

sigma[q_] := -q Log[2, q] - (1 - q) Log[2, 1 - q]
Manipulate[PDF[NormalDistribution[0, sigma[q]], x], {q, 0, 1}]

• I made this change. It does work. However, part of the input is to integrate the PDF function G[x]. It does not make the change in Out[54] whenever I manipulate q. Any ideas?
– sde7
Sep 15, 2018 at 20:23