0
$\begingroup$

Note: I have corrected this question to fix an error in my understanding of RealExponent (which I have now removed).

Suppose that I have a bunch of parameters that I want to set interactively using dynamic controls, all the while updating a plot that shows how those parameters affect a particular calculation. I want to be able to enter those values using something like this:

parameterSelector[param_, initValue_, label_] := 
  DynamicModule[{mant, exp, value, orderOfMagnitude}, {mant, exp} = 
    MantissaExponent[initValue]; value = 10 mant; 
    orderOfMagnitude = exp - Sign[initValue];
    Panel[Column[{Row[{InputField[Dynamic[value], Number, 
      FieldSize -> 14, ContinuousAction -> True], 
      Superscript[10, InputField[Dynamic[orderOfMagnitude], 
      Number, FieldSize -> 6, 
      ContinuousAction -> True]], " = ", 
      Dynamic[NumberForm[N[value 10^orderOfMagnitude]], 6]}], 
      Row[{Slider[Dynamic[value], {-10, 10}], "    ", label}]}]]]

I can call a single instance of this like so:

parameterSelector[k1val, 1.34 10^6, "k1"]

I then envision a column of these that define all of my parameters, like so:

Column[{Dynamic[parameterSelector[k1val, 1.34 10^6, "k1"]], 
  Dynamic[parameterSelector[k2val, 2.86 10^-3, "k2"]]}]

And then to the right of this column, I have my dynamically-updated plot that depends on those parameters. How do I get the dynamically-updated values OUT of this function so that I can then (a) use them in my plot, and (b) return them to the user as their selection from the overall selection panel (I left the dummy variable param in the function definition as a place they can go, but I suppose it could work as a function return too).

Context: This is part of a package I am developing to do chemical kinetic fitting. I have a mechanism for the user to enter coupled differential rate equations with an arbitrary set of variable parameters. I need the user to be able to vary those parameters and see the results of those parameters in a numerical integration (via NDSolve) of these rate equations in order to set an initial starting point for an automatic fitting routine. So the overall user interface will have these parameter-setting panels, a graph that shows the plot as well as experimental data they will be trying to fit the model to (plus residuals), and a button to say "done." The current values of those parameters then get passed back to the user for use as a starting point in FindFit (or similar).

$\endgroup$
0
$\begingroup$

Apparently I am getting in the habit of posting questions too quickly. I apologize.

This question (How to modify function argument?) was a very good source for me.

The solution is as follows:

SetAttributes[parameterSelector, HoldFirst];
parameterSelector[param_, initValue_, label_] := 
  DynamicModule[{mant, exp, value, orderOfMagnitude}, 
    {mant, exp} = MantissaExponent[initValue]; 
    value = 10 mant; 
    orderOfMagnitude = exp - Sign[initValue];
    Panel[Column[{Row[{InputField[Dynamic[value], Number, FieldSize -> 14, 
                                  ContinuousAction -> True], 
                       Superscript[10, InputField[Dynamic[orderOfMagnitude], 
                                  Number, FieldSize -> 6, ContinuousAction -> True]],
                       " = ", 
                       Dynamic[NumberForm[param = 
                                  N[value 10^orderOfMagnitude]], 6]}],
                  Row[{Slider[Dynamic[value], {-10, 10}], "    ", label}]}]]]

Column[{parameterSelector[k1val, 1.34 10^6, "k1"], 
        parameterSelector[k2val, 2.86 10^-3, "k2"], 
        Dynamic[{k1val, k2val}]}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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