I have complex data and which I am fitting to a complex formula. In order to use NonlinearModelFit I split the formula and the data into real and imaginary parts.
As usual NonlinearModelFit is sensitive to initial conditions so I have a DynamicModule to help me find these. My problem is that when inside a DynamicModule NonlinearModelFit keeps running and never stops.
I have made a reduced version of the DynamicModule below to show the effect. The local variable numbers never stop increasing.
Why is this? I feel it should just do one operation and stop.
The data is:
data = {{267.761, 0.666697 - 0.117395 I}, {267.794, 0.720659 - 0.140544 I},
{267.827, 0.78293 - 0.165958 I}, {267.861, 0.850505 - 0.19637 I},
{267.894, 0.933982 - 0.239532 I}, {267.927, 1.04094 - 0.303451 I},
{267.96, 1.15656 - 0.382384 I}, {267.994, 1.30706 - 0.505816 I},
{268.027, 1.48614 - 0.684103 I}, {268.06, 1.69699 - 0.969217 I},
{268.094, 1.89745 - 1.43193 I}, {268.127,1.9794 - 2.19671 I},
{268.16, 1.49939 - 3.27732 I}, {268.193, 0.0979582 - 4.0018 I},
{268.227, -1.38062 - 3.44199 I}, {268.26, -1.96791 - 2.37086 I},
{268.293, -1.95989 - 1.55105 I}, {268.327, -1.77203 - 1.04211 I},
{268.36, -1.572 - 0.722879 I}, {268.393, -1.37354 - 0.5245 I},
{268.427, -1.21688 - 0.402393 I}, {268.46, -1.08912 - 0.310994 I},
{268.493, -0.983056 - 0.249877 I}, {268.526, -0.897219 - 0.205772 I},
{268.56, -0.823682 - 0.173521 I}, {268.593, -0.758923 - 0.150585 I},
{268.626, -0.7049 - 0.127326 I}};
The fitting module is
ClearAll[nlmFit];
nlmFit::usage = "nlmFit[data,{fnest,\[Zeta]est}] uses nonlinear model fit to get \
values for constant (h0r,h0i), residue (gr,gi), natural frequency \
(fn) and damping ratio (\[Zeta]). All data is fitted. Output is the \
standard NonLinearModelFit";
nlmFit[data0_, {fnest_, \[Zeta]est_}] := Module[
{nn, ff, hh, data, model, y, f, hr, hi, rr, ri, fn, \[Zeta]}
,
ff = data0[[All, 1]];
hh = data0[[All, 2]];
nn = Length[ff];
data = Join[
Transpose[{ff, ConstantArray[0, nn], Re[hh]}],
Transpose[{ff, ConstantArray[1, nn], Im[hh]}]
];
model = (1 - y) (( f^2 hr + f (rr - 2 fn hr Sqrt[1 - \[Zeta]^2]) +
fn (fn hr - ri \[Zeta] - rr Sqrt[1 - \[Zeta]^2]))/( f^2 + fn^2 - 2 f fn Sqrt[1 -
\[Zeta]^2])) + y ((f^2 hi + f (ri - 2 fn hi Sqrt[1 - \[Zeta]^2]) + fn (fn hi +
rr \[Zeta] - ri Sqrt[1 - \[Zeta]^2]))/(f^2 + fn^2 - 2 f fn Sqrt[1 - \[Zeta]^2]));
NonlinearModelFit[data,
model,
{{fn, fnest}, {\[Zeta], \[Zeta]est}, rr, ri, hr, hi},
{f, y}]
]
The DynamicModule is
ClearAll[dfit];
dfit[data_, {fn_, \[Zeta]e_}] := DynamicModule[{fitdata},
Dynamic[fitdata = nlmFit[data, {fn, \[Zeta]e}];
fitdata["ParameterConfidenceIntervalTable"]]
]
To run the DynamicModule
dfit[data, {268, 0.0003}]
...and the output never stops changing. Why is the code running again and again? Thanks
DynamicModule[{a}, Dynamic[a = RandomReal[]; a] ]. I'd move assignment outsideDynamic. – Kuba Dec 14 '14 at 12:38Dynamicbut that code has been removed. Based on my understanding, what the OP needs isTrackedSymbols(and possiblyRefresh). – Michael E2 Dec 14 '14 at 13:31fnandZetaEare changed withSlideror something, then there is not need to keep the assignment inside the sameDynamicas the table is. And if it is some kind of automatic loop,Dynamicitself is not really the solution and still I imagine it can be split. However, I do often miss the point of the question ;P – Kuba Dec 14 '14 at 13:36fitdatais assigned a new value insideDynamicand it is a tracked symbol. – Michael E2 Dec 14 '14 at 13:45DynamicModule[{a}, Dynamic[a = RandomReal[]] ]but the behaviour is different. Moreover, without DM it is smarter too:Dynamic[a = RandomReal[]; a]. – Kuba Dec 14 '14 at 13:56Random*function "are not ticklish," and (2) a mystery reason. :) My guess about the difference whenais a front-end DynamicModule variable and whenais a kernel variable is complicated, but has to do with which "owns" the variable. – Michael E2 Dec 14 '14 at 14:05RandomRealexamples behave the way they do). – Michael E2 Dec 14 '14 at 14:18Dynamic[<code>, TrackedSymbols :> {}]orDynamic[<code>, TrackedSymbols :> {x, y,...}]wherex,y,... are your slider variables, but notfitdata. The reason is explained (imo) in some answers to the linked questions. – Michael E2 Dec 14 '14 at 14:45