0
$\begingroup$

I am trying to manipulate a plot of the Bernoulli roots as follows:

enter image description here

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.

$\endgroup$

3 Answers 3

1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Wonderful - thank you very much for your help! $\endgroup$
    – martin
    Commented Oct 18, 2013 at 19:18
1
$\begingroup$
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}]
$\endgroup$
1
  • $\begingroup$ Great - many thanks, I will have to use plotRange on this as advised by Anon - but the code works well :) $\endgroup$
    – martin
    Commented Oct 18, 2013 at 19:19
1
$\begingroup$

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.

$\endgroup$

Your Answer

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

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