1
$\begingroup$

I'm having a problem with DynamicModule; the code below produces a plot with a line that changes value, based on the slider positions of the parameters, which is very nearly what I want; I would simply like that line to have slightly more bends, and to alter those bends properly, in accordance with the formula it is ostensibly drawing from.

I've read through the dynamic introduction, the DynamicModule page and the advanced functionality documentation, and while I have an idea as to the issue, I'm not at all convinced it is the sole culprit, nor do I have ideas as to the solution

I suspect that by making r the argument of module (as it must be, to avoid global contamination), I am preventing the function from evaluating it properly, leading to the titular problem. Another theory is that I have somewhere changed the Head of something in a way that is leading to MMA refusing to evaluate the function, or done something invalid scoping-wise, in which case I may be stuck. I have not found anything that would recommend a solution thus far.

Slider[Dynamic[a]] "a = " Dynamic[a]
Slider[Dynamic[b]] "b = " Dynamic[b]
Slider[Dynamic[s]] "\!\(\*SubscriptBox[\(N\), \(0\)]\) = " Dynamic[s]


DynamicModule[ {r},

\[Eta][r_] = s (r^((1 - 3 b)/b) E^(-r/(a  b)));


Dynamic[Plot[\[Eta][r], {r, 0, 10}]]]

What I do not currently understand is why this is not producing a graph of the shape r^-number E^(-r/number) that shifts slightly when the sliders for the constants are altered, as is my goal (this is a size distribution, and I'd like to provide readers of the notebook an idea of what the size distro looks like while simultaneously using that distribution in the calculation of important data).

Thus far, I have tried: recasting the dynamic plot into a manipulate, switching the ordering of dynamic and plot, placing the sliders inside the dynamic module, breaking the module and removing r as a dynamic variable (something not feasible for the long term, considering the necessity of manipulating other functions).

Furthermore, this snippet (with the aid of the aforewritten sliders, which work just fine) produces the result I'm looking for, shape wise and behavior wise, but obviously uses a global variable as opposed to a module one.

TestFunction[p_] := s p^((1 - 3 b)/b) E^(-p/a b)
Dynamic[Plot[testFunction[p], {p, 0, 100}]]

However wrapping this in DynamicModule with first argument p produces 'Null', reinforcing my theory that the nature of dynamic module is preventing the function from being evaluated; however I don't see how a local variable would affect the function as written, especially as it is defined within the module itself. Furthermore if this is the case, I'm not sure how to resolve the issue of keeping one local value for a function so that it can be graphed while simultaneously having other functions (Which rely on r being a variable, due to them integrating over it; MMA very much dislikes the dr part of an integral if r has a numerical value) use it.

I am all but sure that I am missing something painfully obvious as to how to create a module to do the same thing, but thus far, searches (through stackexchange and the almighty google) and testing have been fruitless.

I do hope I have been sufficiently specific, as this is my first stack-exchange query after much lurking; if there is any vital information I have neglected to provide, please let me know.

$\endgroup$
0

1 Answer 1

3
$\begingroup$
Clear[η, a, b, s]
η[r_, s_, a_, b_] := s (r^((1 - 3 b)/b) E^(-r/(a b)))

Manipulate[
  Plot[η[r, s, a, b], {r, 0, 10}, PlotRange -> All],
  {{s, 0.8}, 0, 1, Appearance -> "Labeled"},
  {{a, 0.7}, 0.1, 1, Appearance -> "Labeled"},
  {{b, 0.05}, 0.001, 0.1, Appearance -> "Labeled"}
]

Mathematica graphics


Regarding your original code, when you want to format/organize different elements, consider using Row, Column, or Grid. For instance:

Slider[Dynamic[a]] "a = " Dynamic[a]

Your original code above actually represents a multiplication of the slider by a string by the dynamic value of a. It just serendipitously appears to work in this case, but it is definitely not advisable. A better approach would be:

Row[{"a = ", Dynamic[a], Slider[Dynamic[a]]}]

Mathematica graphics

Perhaps a more visually pleasing option would be to control the format of the value of a so the slider doesn't jump around, and introduce some spacing between the value and the slider itself:

Row[{"a = ", Dynamic[NumberForm[a, {4, 3}]], Spacer[10], Slider[Dynamic[a]]}]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Appreciation abounds for this far more elegant (and quicker, as it seems to be) solution than what I had been thinking of. I wasn't properly considering a multivariable function as a solution due to the presence of eta in other functions which were at that point already singly defined in r. Sometimes is pays to have somebody else look at things! I shall take the formatting advice to heart, and fix my sliders post-haste. I'm still getting used to the relative paucity of delimiters in Mathematica as compared to other languages, so I'll be more careful with formatting in the future. Thanks again! $\endgroup$
    – Ordonis
    Commented Jul 20, 2016 at 0:39
  • $\begingroup$ @Ordonis I'm very glad I could help. $\endgroup$
    – MarcoB
    Commented Jul 20, 2016 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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