2
$\begingroup$

I've made a peak-fitting GUI in which I have a "Fit" button that, when pressed, calls NMinimize[...,StepMonitor->(step++)] to minimize a sum of squared deviations. Elsewhere in the GUI I have a Dynamic[ToString[step]] so that the user can monitor the progress of the fitting. For whatever reason, the string only gets updated at the end of the NMinimize computation and not during.

Rather than post my own enormous code here, I've put together a simple example that exhibits the same behavior:

vars = {0, 0, 0, 0};
{
Button["DO STUFF", (

data = {
       {+0.18,-0.13}, {+0.84,-0.06}, {+0.05,+0.88}, {+0.24,-0.63}, {+0.67,+0.93}, 
       {+0.05,+0.88}, {+0.65,+0.92}, {+0.01,+0.99}, {+0.17,-0.04}, {+0.23,-0.55}
       };

model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];

FindFit[
       data,
       model[{a, k, w, p}],
       {a, k, w, p},
       x,
       StepMonitor :> (vars = {a, k, w, p}; Pause[0.3];)
       ]
)],
Dynamic[vars]
}

If I evaluate the above code and press the button, nothing happens for a while and then suddenly the Dynamic[vars] updates with the final value of vars.

If, on the other hand, I remove the code from the button and evaluate it on its own then the Dynamic[vars] updates as expected:

vars = {0, 0, 0, 0};

data = {
       {+0.18,-0.13}, {+0.84,-0.06}, {+0.05,+0.88}, {+0.24,-0.63}, {+0.67,+0.93}, 
       {+0.05,+0.88}, {+0.65,+0.92}, {+0.01,+0.99}, {+0.17,-0.04}, {+0.23,-0.55}
       };

model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];

FindFit[
       data,
       model[{a, k, w, p}],
       {a, k, w, p},
       x,
       StepMonitor :> (vars = {a, k, w, p}; Pause[0.3];)
       ];

Dynamic[vars]

The same thing happens if I replace the Button[] with a homemade button using EventHandler[]. Can anybody tell me why this happens and if there's a way around it? I suspect that it's a scope issue or a front end vs. kernel thing but I don't know much about Mathematica under the hood.

Thanks in advance for any help that may be offered!

P.S. I'm using MMA version 8.0 on an Intel Mac.

$\endgroup$
1
  • 1
    $\begingroup$ The disappearance of your opening greeting isn't a glitch... StackExchange just disagrees with the inclusion of pleasantries in questions. Anyway, welcome to the site! $\endgroup$ Commented Mar 16, 2013 at 2:43

1 Answer 1

3
$\begingroup$

Simple solution is: you have to use Method->"Queued" as option to button

vars = {0, 0, 0, 0};
{Button["DO STUFF", (data = {
{+0.18, -0.13}, {+0.84, -0.06}, {+0.05, +0.88}, {+0.24, -0.63}, 
{+0.67, +0.93}, {+0.05, +0.88}, {+0.65, +0.92}, 
{+0.01, +0.99}, {+0.17, -0.04}, {+0.23, -0.55}};
   model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];
   FindFit[data, model[{a, k, w, p}], {a, k, w, p}, x, 
    StepMonitor :> (vars = {a, k, w, p}; Pause[0.3];)]), 
  Method -> "Queued"], Dynamic[vars]}
$\endgroup$
2
  • $\begingroup$ Thank you. I looked at Method->"Queued" in the documentation but I didn't think it was applicable. It certainly works for this example. I'll try it on my larger code and see if it fixes the problem. Many thanks! $\endgroup$
    – Adam W.
    Commented Mar 15, 2013 at 23:44
  • $\begingroup$ Works great in my larger code as well. Thanks again. $\endgroup$
    – Adam W.
    Commented Mar 15, 2013 at 23:55

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