# Evaluation of global variables and function arguments inside Manipulate[Plot[]] - simple problem [duplicate]

I'd like to pass functions / expressions into Manipulate[ComplexPlot[...]...] and have Manipulate update some of the values and then update the plot correspondingly.

For instance:

In[161]:= ClearAll[f, n]

In[172]:= f[z_] := z^n

In[174]:= n = 2

Out[174]= 2

In[175]:= f[I]

Out[175]= -1

In[176]:= f[z]

Out[176]= z^2

In[177]:= Manipulate[ ComplexPlot[f[z], {z, -(2 + 2 I), (2 + 2 I)}], {n, 1, 5}]

The above makes a nice colorful plot with a slider to change the value of n, but when I move the sider the plot does not update... [see image below]

OTOH:

Manipulate[ComplexPlot[z^n, {z, -(2 + 2 I), (2 + 2 I)}], {n, 1, 5}]

Makes a nice plot that updates as I move the slider.

But I want to pass in functions to ComplexPlot inside of Manipulate, so that I can define them outside of Manipulate[].

Is there an easy to understand and concise way to do this? One that doesn't involve a lot of deep knowledge of Hold, Evaluate, @, #, etc :-)

## 1 Answer

Here is one way

ClearAll[f, n]
f[z_, n_] := z^n
Manipulate[ComplexPlot[f[z, n], {z, -(2 + 2 I), (2 + 2 I)}],
{{n, 1, "n"}, 1, 5, 1, Appearance -> "Labeled"},
TrackedSymbols :> {n}]


As general rule, it is bad to use global variables. (This is true in any language and not just Mathematica). Always pass the variables the function needs to use as function parameters. Also add the list of the control variables that Manipulate needs to track.

Only the variables that explicitly appear inside the Manipulate expression are tracked by Manipulate by default. But better be explicit using TrackedSymbols. Changes in variable outside Manipulate by default have no effect on Manipulate. (but this can be changed if needed, but better not to).