Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
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}]
share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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