Correct placement of Dynamic and related functions

Hello all. I am not an experienced Mathematica user; been using it for the past year or so. I am completely self-taught.

I have some successful and useful notebooks that I have been using. I then got the idea of exporting them as CDFs to share with others and discovered that I am doing a lot wrong. The first issue was simple: add “SaveDefinities → True” to all my Manipulate statements. Worked fine if all my control objects are kept within the Manipulate function. However, I tried to get a bit fancier and then ran into trouble. I am completely lost where “Dynamic” (or related) functions belong.

I have tried to condense what I working on into a skeleton script, below. As a CDF, when this file first opens up, it doesn’t know default values for v0m, viu, etc. How do I correctly specify them? Feel free to also comment/criticize on my overall approach. I now this script doesn’t make any sense; it is just a mock up of my much larger script, which I am happy to share if people want.

Thanks in advance!

cal[v0m_, viu_, maxI_] := (v0m + viu + maxI);
v0m = 1;
viu = 2;
maxI = 3;
model = 1;
resLigand = False;
Grid[{
{"Binding Model",
SetterBar[
Dynamic[model], {1 -> "One Site", 2 -> "Two Independent Sites",
3 -> "Two Cooperative Sites"}]},
{"Residual Ligand?", Checkbox[Dynamic[resLigand]]},
{"Cell Volume (mL)", InputField[Dynamic[v0m]]},
{"Injection Volume (uL)", InputField[Dynamic[viu]]},
{"Number of Injections", InputField[Dynamic[maxI], Number]},
{Dynamic@Switch[model + Switch[resLigand, False, 0, True, 3],
1, Manipulate[(output = cal[v0m, viu, maxI];
output + model + variable +
Switch[resLigand, False, 0, True, 3]), {variable, 0.1, 1.0}],
2, Manipulate[(output = cal[v0m, viu, maxI];
output + model + variable +
Switch[resLigand, False, 0, True, 3]), {variable, 0.1, 1.0}],
3, Manipulate[(output = cal[v0m, viu, maxI];
output + model + variable +
Switch[resLigand, False, 0, True, 3]), {variable, 0.1, 1.0}],
4, Manipulate[(output = cal[v0m, viu, maxI];
output + model + variable +
Switch[resLigand, False, 0, True, 3]), {variable, 0.1, 1.0}],
5, Manipulate[(output = cal[v0m, viu, maxI];
output + model + variable +
Switch[resLigand, False, 0, True, 3]), {variable, 0.1, 1.0}],
6, Manipulate[(output = cal[v0m, viu, maxI];
output + model + variable +
Switch[resLigand, False, 0, True, 3]), {variable, 0.1, 1.0}]
]}
}]

Welcome to Wolfram Community, Michael! Please note how your code was formatted in special code-blocks. This makes it easy to read it and copy it. Use Spikey button in the editor - see image below. If you could also upgrade your profile information - it’d be great!

ScreenShot2013-07-23at1.14.16PM.png

Vitaly,

Yes, I had a problem using the special code block. When I first opened the window, all the buttons were available. I typed my question with the intention of putting the code at the bottom of the page. At that point, many of the buttons (including the spikey button) was grayed-out. The only thing I could hit was “Source”, which affected the entire message. I am not sure what I did wrong.

Michael

Hi Michael,

I looked a little bit into Your code and I think what You are trying to do is something like this example:

g[x_] := Exp[x]
f1[x_] := x^2
f2[x_] := x^3
DynamicModule[{t = 1, f = f1, s = 0, boolvar = False}, 
 Grid[{{SetterBar[
     Dynamic[f], {f1 -> "Square Function", 
      f2 -> "Cubic Function"}]}, {Checkbox[
     Dynamic[boolvar]]}, {InputField[Dynamic[t]]}, {Slider[
     Dynamic[s]]}, {Evaluate[
     Dynamic[f[t + s] + If[boolvar, g[t], 0]]]}}]]

Maybe You don’t know this, but You can switch whole functions dynamically like I have done with f1 and f2. You do not need to use 6 Switch-Cases. The second thing is, that you can initialize variables which you only need locally, i.e. inside a Module block - or in this case a DynamicModule block.
Using Module blocks to create local variables is quite nice so you avoid interfering with later computations.

I hope this is helpful!

Manuel