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.