I am using a loop to run run multiple fits. If a fit fails, I don't want it to either appear in my data or to affect later fits so I am using the Check[] function to catch if any errors occurred (I'm especially looking out for the "Failed to converge to the requested accuracy or precision..." message).
So the code I have is (I know Do loops are frowned upon, but it's easier for me to read; I'm not really a Mathematica programmer):
klist = Import["klist.dat", "List"]
sklist = Import["sk.dat", "List"]
phiprime[w_, wL2_, wk2_, t_, \[Gamma]_] := (wL2 - \[Gamma]*wk2)*t*Exp[-t^2*w^2/Pi]
phidprime[w_, wL2_, wk2_, t_, \[Gamma]_] := 2*t/Sqrt[Pi]*(wL2 - \[Gamma]*wk2)*DawsonF[t*w/Sqrt[Pi]]
foo[w_, wL2_, wk2_, t_, sk_] := sk/Pi*wk2*phiprime[w, wL2, wk2, t, 1]/((w^2 - wk2 - w*phidprime[w, wL2, wk2, t, 1])^2 + (w*phiprime[w, wL2, wk2, t, 1])^2)
fit1[sk_, dat_, wL20_, wk20_, t0_] := NonlinearModelFit[dat, foo[w, wL2, wk2, t, sk], {{wL2, wL20}, {wk2, wk20}, {t, t0}}, w]
inputs = Import["inputs.dat", "Table"]
tau = 900
outputs = {}
Do[
k = klist[[i]];
sk = sklist[[i]];
skwdata = Import["skw.k_" <> StringTrim[ToString[PaddedForm[k, {4, 3}]]] <> ".new.dat", "Table"];
Check[temp = Transpose[fit1[sk, skwdata, inputs[[i]][[2]], inputs[[i]] [[3]], tau]["BestFitParameters"] /. Rule -> List][[2]];
outputs = AppendTo[outputs, temp];
tau = temp[[3]],
AppendTo[outputs, {0, 0, 0}]],
{i, 1, Length[klist]}
]
Here fit1 is a defined function that uses NonlinearModelFit, so temp will end up being a list of the the three best fit parameters.
What I imagined the Check statement thus implemented would do was, if everything went smoothly, return the first three statements, and if not (instead of writing the best-fit parameters) write zeroes to the list outputs. Instead, it doesn't seem to do anything special when the "Failed to converge..." message is thrown. {0,0,0} is indeed added to ouputs, but the three fitting parameters for the failed fit are still written to outputs and tau is still changed.
What needs to be changed to make this work?
AppendTo[outputs, {0, 0, 0}]
asAppend
will not changeoutputs
. $\endgroup$