Message Boards Message Boards

2
|
6591 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Manipulate - switch on/off curves in a Plot

I have 4 functions. I want them to show up in a plot where I can switch on and off curves:

f1[x_] := Cos[3 Sqrt[x]]
f2[x_] := Sin[x]
f3[x_] := Sin[2 x]
f4[x_] := Sin[.2 x^2]

Manipulate[
 Plot[{g1[x], g2[x], g3[x], g4[x]}, {x, 0, 10}], {{g1, f1, 
   ""}, {f1 -> "I1", "0"}}, {{g2, "0", ""}, {f2 -> "I2", "0"}}, {{g3, 
   "0", ""}, {f3 -> "I3", "0"}}, {{g4, "0", ""}, {f4 -> "I4", "0"}}, 
 ControlPlacement -> Left]

The behavior is correct in the code above. However, it is easy to see that a more elegant solution would be one with four CheckBoxes, but I can't figure out how to make this work properly. Is there anybody who knows how to do this? Thanks for your help Maarten

2 Replies

Dear Nasser, Thanks, this helps. One small deficiency in your solution however, the curves change color depending on the order you click the check boxes. I would like to see the Cos[3 Sqrt[x]] in blue, the Sin[x] in orange, etc. independent of the order the check boxes are checked or unchecked. I might be able to work that out. Thanks - Maarten

updated: make colors specific to each function.

enter image description here

Manipulate[Plot[Evaluate@f[[choice]], {x, 0, 10}, PlotStyle -> color[[choice]]], 
 Row[{CheckboxBar[Dynamic[choice], {1, 2, 3, 4}, Appearance -> "Vertical"]}], {{choice, {1, 2, 3, 4}}, None}, 
 ControlPlacement -> Left, Initialization :> (
   f = {Cos[3 Sqrt[x]], Sin[x], Sin[2 x], Sin[0.2 x^2]};
   color = {Blue, Red, Green, Black};
   )
 ]
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract