1
$\begingroup$

I have an interactive plot in a Manipulate expression with a PopupMenu. I want to be able to select the function being plotted with the popup. When I use the PopupMenu the other controls (sliders) stop working. I am unable to find the problem.

Manipulate[
  Plot[f[x], {x, -10, 10}, PlotRange -> {-10, 10}], 
  {f, {a # + b & -> "Linear", Abs [a # + b] & -> "Absolute Value"}, 
    ControlType -> PopupMenu}, 
  {a, -3, 3, 1, Appearance -> "Labeled"}, 
  {{b, -5, "y-intercept"}, 5, 1, Appearance -> "Labeled"}]

I have seen several examples for PopupMenu and SetterBar. But none where parameters in a dynamically selected function can be changed by other controls.

$\endgroup$
  • $\begingroup$ Moving your rules to the Plot command does what I think you want. Manipulate[ Plot[f[x] /. {a -> $a, b -> $b}, {x, -10, 10}, PlotRange -> {-10, 10}], {f, {a # + b & -> "Linear", Abs[a # + b] & -> "Absolute Value"}, ControlType -> PopupMenu}, {$a, -3, 3, 1, Appearance -> "Labeled"}, {{$b, -5, "y-intercept"}, 5, 1, Appearance -> "Labeled"}] $\endgroup$ – bobthechemist Mar 29 '15 at 22:36
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Mar 29 '15 at 22:37
  • $\begingroup$ Another possibility is Manipulate[ Dynamic@Plot[f[x], {x, -10, 10}, PlotRange -> {-10, 10}], {f, {a # + b & -> "Linear", Abs[a # + b] & -> "Absolute Value"}, ControlType -> PopupMenu}, {a, -3, 3, 1, Appearance -> "Labeled"}, {{b, -5, "y-intercept"}, -5, 5, 1, Appearance -> "Labeled"}]. The plot needs to be dynamic in order to be updated in this fashion. If I come up with a good explanation why, I'll post an answer rather than a comment. $\endgroup$ – bobthechemist Mar 29 '15 at 22:43
  • $\begingroup$ ...and it looks like this issue has been addressed previously. It seems to deal with the visibility of symbols. $\endgroup$ – bobthechemist Mar 29 '15 at 22:52
  • $\begingroup$ Thank you very much...the following worked with no $ on the slider. Manipulate[ Plot[f[x] /. {a -> $a, b -> $b}, {x, -10, 10}, PlotRange -> {-10, 10}], {f, {a # + b & -> "Linear", Abs[a # + b] & -> "Absolute Value"}, ControlType -> PopupMenu}, {a, -3, 3, 1, Appearance -> "Labeled"}, {{b, -5, 5, 1, Appearance -> "Labeled"}] $\endgroup$ – esp Mar 29 '15 at 23:04
1
$\begingroup$

You have a few small mistakes which are easily fixed.

Manipulate[
  Dynamic @ Plot[f[x], {x, -10, 10}, PlotRange -> {-10, 10}], 
  {{f, a # + b &}, {a # + b & -> "Linear", Abs[a # + b] & -> "Absolute Value"}, 
    ControlType -> PopupMenu},
  {{a, -2} -3, 3, 1, Appearance -> "Labeled"}, 
  {{b, -3,}, -5, 5, 1, Appearance -> "Labeled"}]

demo

Here are the fixes.

  1. Plot needs to wrapped in Dynamic so the plot will updated when the controls change.
  2. The popup menu needs to be initialized just like the other controls.
  3. The animator for parameter b needs to have it range specified correctly.

And, of course, it is mis-labelling to call b the y-intercept when Abs is plotted,

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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