I have performed a nonlinear fit onto a set of data according to the form $a+b\cos(2\pi x)+c\sin(2\pi x)+d\cos(4\pi x)+e\sin(4\pi x)$. How do I access the best fit parameters $a,b,c,d,e$, store them into a list perhaps?
2 Answers
Once you have the model
model = NonlinearModelFit[data, a + b Cos[2π x] + c Sin[2π x] + d Cos[4π x] + e Sin[4π x],
{a, b, c, d, e}, x]
you can extract individual parameters with, for example,
a /. model["BestFitParameters"]
(* a real number *)
or all of them at once in the order you like,
{a, b, c, d, e} /. model["BestFitParameters"]
(* list of five real numbers *)
I'd like to point out that your fit is actually a linear one, so you can do more efficiently
model = LinearModelFit[data, {1, Cos[2π x], Sin[2π x], Cos[4π x], Sin[4π x]}, x]
and get the list of best-fit parameters in the same order as the list of functions,
model["BestFitParameters"]
(* list of five real numbers *)
If you name model=NonlinearModelFit[...]
model["BestFitParameters"]
is what you are looking for I think.
-
$\begingroup$ Thank you, but how would I store the individual values of $a,b,c,d,e$ into lists? $\endgroup$ Jun 4, 2019 at 6:34
-
1$\begingroup$
Values[nlm["BestFitParameters"]]
creates the list! $\endgroup$ Jun 4, 2019 at 6:41