Message Boards Message Boards

[✓] Put a nicely formatted label in front of the sliders in a Manipulate?

GROUPS:

Hi, I am new to the wolfram way of doing things. I've been messing around with it for about a week with the online version before getting angry with the lack of keyboard shortcuts and formatting options and moving to the desktop version a couple days ago. Anyway I'm a chemist and looking to start building interactive modules/tutorials for my students. Maybe even full out course note packs built here (hence the need for all the formatting options without hassle).

Anyway, I decided to start with the supposedly trivial problem of plotting an acid dissociation plot for some arbitrary diprotic acid, H2A <=> HA- <=> A2- having dissociation constants of pK1 and pK2. The Y-axis is the fraction (0-1) of a given species present, and the X-axis is pH (0 to 14) - I haven't tried to battle with labeling the axes yet. a0, a1, a2 are the fractions of H2A, HA-, and A2- present at a given pH. Please excuse the lack of elegance in my code - also I know uppercase is usually reserved for built-in functions, but soon I'll need k and K for expressions and want to keep the standard definitions so that my students can maybe follow along.

So we have for some initial definitions of variables and equations:

h := 10^-pH
n := 2
K1 := 10^-pK1
K2 := 10^-pK2
d := h^n + K1*h^(n - 1) + K1*K2
a0 := h^n/d
a1 := K1*h^(n - 1)/d
a2 := K1*K2/d

where h is the concentration of H+ (10^-pH), K1 and K2 are the equilibrium constants for the two dissociations, d is a denominator term, n is the number of protons in the acid (eventually I will be looking at generalizing this more to polyprotic acids)

Anyway, if I use the following code to continue on, I get a pair of sliders nicely labeled which can manipulate pK1 and pK2, but the plot is completely frozen.

Manipulate[
 Plot[{a0, a1, a2}, {pH, 0, 14}, PlotLegends -> "Expressions"], 
    {{pK1, pK1, "pK1: "}, 0, pK2, 0.05, Appearance -> "Labeled"}, 
    {{pK2, pK2, "pK2: "}, pK1, 14, 0.05, Appearance -> "Labeled"}]

There is clearly some little thing somewhere that I am missing with how Manipulate works, or maybe it is that the variable you are manipulating must appear directly in the expression being manipulated. None of the examples of Manipulate that I've been able to find have you manipulating a variable that is not directly used in the expressions being manipulated and pK1 and pK2 don't show up directly in the equations for a0, a1, a2. In any event, while trying to fix this I came across the Slider[Dynamic[x]], and Dynamic[Plot[...]] trick. That led me to the following code which works great, except for the life of me I cannot figure out how to get labels of "pK1: " and "pK2: " to show up in front of the sliders without putting them in a list with the slider {label, slider} which leaves braces around the output which looks ugly.

Slider[Dynamic[pK1], {0, pK2, 0.05}, Appearance -> "Labeled"]
Slider[Dynamic[pK2], {pK1, 14, 0.05}, Appearance -> "Labeled"]
Dynamic[Plot[{a0, a1, a2}, {pH, 0, 14}, PlotLegends -> "Expressions"]]

Any hints/explantion on why Manipulate was not working to manipulate the plot in the first instance would be appreciated, as would tips for how to put a nicely formatted label in front of the sliders in the second attempt at plotting.

Thanks,

James :)

POSTED BY: James Harynuk
Answer
2 months ago

Manipulate works on symbol explicitly present in the expression. This code does what you want (I when for the quick fix and mutilated your variable names—sorry!)

Manipulate[
 Plot[Evaluate[{a0, a1, a2} /. {pK1 -> p1, pK2 -> p2}],
  {pH, 0, 14}, PlotLegends -> "Expressions"],
 {{p1, 1, "pK1: "}, 0, p2, 0.05, Appearance -> "Labeled"},
 {{p2, 10, "pK2: "}, p1, 14, 0.05, Appearance -> "Labeled"}
 ]

Alternatively, you could make a functional definition of a0, a1, a2 and call them as a0[pK1, pK2], ...

POSTED BY: Giulio Alessandrini
Answer
2 months ago

Thank you very much, Giulio!

That works just great! I will read up about this Evaluate command as well... it looks like a very useful command as well. I may also try the functional definition route as well just for practice and my own education - much reading to do and not enough time!

James

POSTED BY: James Harynuk
Answer
2 months ago

Group Abstract Group Abstract