Jim Baldwin's remark about the signs of the parameters suggests trying the following:
NonlinearModelFit[data,{temp2[t],a>0&&k2>0},{a,k2},t]["BestFitParameters"]
(* {a->866.073,k2->0.0300234} *)
Other ways to find starting values
[I'd worked out the following before trying the above.]
In this case, because of the model and the data, one can solve for the parameters that give the model a maximum where the data has a maximum. For noisier data, one might use FindPeaks[data, σ]
, with an appropriate smoothing parameter σ
; or one could estimate the maximum by eye, if it's there are too many cases to deal with. The parameter values for the approximate initial model are obtained by solving, not by fitting, and equations are constructed for the particular model at hand. The solutions should make good starting values for FindFit
or NonlinearModelFit
.
kInit=k2/.First@NSolve[temp2''[t]<-Sqrt@$MachineEpsilon&&temp2'[t]==0/.t->First[dmax]/.a->1,k2]
aInit=a/.First@NSolve[temp2[t]==Last[dmax]/.k2->kInit/.t->First[dmax],a]
FindFit[data,temp2[t],{{a,aInit},{k2,kInit}},t]
(*
0.0278868
954.695
{a->866.073,k2->0.0300234}
*)
Also, some thought makes it clear (if I was thinking clearly) that k2
should be greater, but not too much greater, than the other exponential coefficient, 0.01376208490662561
.
FindFit[data,temp2[t],{a,{k2,2*0.01376208490662561}},t]
(* {a->866.073,k2->0.0300234} *)
Or:
FindFit[data,{temp2[t],k2>0.01376208490662561},{a,k2},t]
(* {a->866.073,k2->0.0300234} *)
Spelunking
Sometimes I want to be sure I know what's going on inside. I don't know a direct way to obtain the function that NonlinearModelFit[]
minimizes. Here is an indirect way:
(* returns an Experimental`NumericFunction[] *)
nf = Head@Trace[
NonlinearModelFit[data,temp2[t],{a,k2},t,Method->"NMinimize"],
HoldPattern[NMinimize[obj_,___]]:>Return[obj,Trace],
TraceInternal->True];
obj=nf["FunctionExpression"] (* gets the (long) expression for the function *)
FindMinimum[{obj,a>0&&k2>0},{a,k2}]
(* {146.814, {a->866.073,k2->0.0300234}} *)
I assume the objective function is the same no matter what Method
is used to minimize it.