# Manipulate syntax

I am trying to manipulate a plot of the Bernoulli roots as follows: The following code clearly is not going to work, but I have included it in an effort to illustrate my meaning.

sol = Solve[N[Table[BernoulliB[n, z], {n, 20, 20}] == 0, 10]];
Manipulate[ListPlot[{{Re@z, Im@z} /. sol}], {roots, 1, 100}]


It is the {n, 20, 20} that I think I am having the trouble with - the 20 is obviously the variable I would like to manipulate, and {n, roots, roots} was all I could think of doing.

I am also unsure how to format the axes and point sizes so they remain constant throughout the manipulation.

Like this:

Manipulate[
sol = Solve[N[Table[BernoulliB[n, z], {n, roots, roots}] == 0, 10]];
ListPlot[{{Re@z, Im@z} /. sol},
PlotRange -> {{-10, 10}, {-10, 10}}], {roots, 1, 100, 1}]


PlotRange is used to keep the axes constant.

• Wonderful - thank you very much for your help! – martin Oct 18 '13 at 19:18
Manipulate[
sol = Solve[N[BernoulliB[x, z] == 0, 10]];
ListPlot[{{Re@z, Im@z} /. sol}] , {{x, 10}, 1, 100, 1}]


In case you meant to plot a range of n:

Manipulate[
sol = Table[Solve[N[BernoulliB[n, z] == 0, 10]], {n, x, y}];
ListPlot[ {Re@z, Im@z} /. sol]
, {{x, 10}, 1, Dynamic[y], 1}, {{y, 12}, Dynamic[x], 100, 1}]

• Great - many thanks, I will have to use plotRange on this as advised by Anon - but the code works well :) – martin Oct 18 '13 at 19:19

Use NSolve and make the vars argument z instead of 10 and you'll notice a big improvement in performance, especially when roots is large.

Manipulate[
ListPlot[{Re@z, Im@z} /. NSolve[BernoulliB[roots, z] == 0, z],
PlotRange -> {{-10, 10}, {-10, 10}}],
{roots, 1, 100, 1}]


I got rid of the global variable sol because there are issues with having global variables in a Manipulate. Those issues can be managed, but when possible, localize variables. If you need sol to use in multiple places, then With or Module is an appropriate way to localize a temporary variable. With may be replaced by Module if sol needs to change its value several times during a single update.

Manipulate[
With[{sol = NSolve[BernoulliB[roots, z] == 0, z]},
ListPlot[{Re@z, Im@z} /. sol, PlotRange -> {{-10, 10}, {-10, 10}}]
],
{roots, 1, 100, 1}]


In some cases, it is better to localize a variable in the DynamicModule constructed by Manipulate. This can be done by specifying the control type None (see this answer for some discussion):

Manipulate[
sol = NSolve[BernoulliB[roots, z] == 0, z];
ListPlot[{Re@z, Im@z} /. sol, PlotRange -> {{-10, 10}, {-10, 10}}],
{roots, 1, 100, 1}, {sol, None},
TrackedSymbols :> {roots}]


We have to use TrackedSymbols or Manipulate will update twice for each change in roots: once when roots changes value and another time when sol changes value. When With above is not appropriate, this tends to be my favorite way, although in this particular example, using Module instead of With is just as good.