# How to include Initialization in DynamicModule

I want to learn some of the basic rules for construction DynamicModule code. I have looked at Kuba's post and feel overwhelmed by it. My ultimate goal is to use Switch in a DynamicModule that will provide different GUI's, each of which would plot equations produced by different algorithms.

So I began my journey by trying to transform the following Manipulate code to DynamicModule code:

Manipulate[
Plot[Evaluate[x[t, u]], {t, 0, 2}, PlotRange -> {-1, 1},
Exclusions -> None],
{{u, 1, " "},  {1 -> A, 2 -> B, 3 -> C}},
TrackedSymbols :> {u, f},
Initialization :> (
x[t_, u_] := Switch[u,
1, Sin[2 \[Pi] t],
2, SquareWave[t],
3, TriangleWave[t]];
)
]


The DynamicModule code that I came up with doesn't work. For starters, I dpn't know where to place the Initialization code. As you can see my my futile attempt, I lack understanding of the basic structure of DynamicModule.

DynamicModule[{},
Dynamic@Plot[x[t, u], {t, 0, 2}, PlotRange -> {-1, 1},
Exclusions -> None],
{{u, 1, " "}, {1 -> A, 2 -> B, 3 -> C}},
Initialization :> (
x[t_, u_] := Switch[u,
1, Sin[2 \[Pi] t],
2, SquareWave[t],
3, TriangleWave[t]];
)
]


Some guidance is needed.

• That topic is quite old and may be vague, I will have to rephrase all that in context of what I've learned about GUIs creation in MMA. But going back to the question, Initialization is in a good place but you have to take care about Controls spec. While Manipulate translates {u, 1, " "} to GUI and puts it next to the body, in DynamicModule you have to do this manually. Use Grid/Row/Column/Panel for positioning and Slider/PopupMenu and friends for those controllers.
– Kuba
Mar 8, 2016 at 22:01
• If you are not very familiar with Dynamic functionality, my advice is to start off with Manipulate. It's fine for simpler GUIs for personal use, for class demonstrations, and so forth. However, if you keep running up against the limitations of Manipulate (and not just the limits of your knowledge), or if you are creating a commercial or business product, in which reliability and robustness are important, then you will need the control over things that you get with DynamicModule. Mar 8, 2016 at 23:08

As indicated by Kuba you have to take charge of the controls when DyanmicModule is used.

Here is a simple example from the documentation on DyanmicModule.

DynamicModule[{x}, {Slider[Dynamic[x]], Dynamic[x]}]


For your particular example below is a method that uses Column. Note that using Initialization will set the global value of the function x.

DynamicModule[
{
u
},

Column[{
Dynamic@Plot[Evaluate[x[t, u]], {t, 0, 2},
PlotRange -> {-1, 1},
Exclusions -> None,
ImageSize -> 400],

SetterBar[Dynamic[u], {1 -> A, 2 -> B, 3 -> C}]
}, Alignment -> Center
],

Initialization :> (
x[t_, u_] := Switch[u,
1, Sin[2 \[Pi] t],
2, SquareWave[t],
3, TriangleWave[t]
]
)

]


• Very helpful. Your example was much better than any of the others I've seen in the Wolfram documentation. Mar 9, 2016 at 3:28