0
$\begingroup$

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?

$\endgroup$

2 Answers 2

2
$\begingroup$

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    *)
$\endgroup$
1
$\begingroup$

If you name model=NonlinearModelFit[...]

model["BestFitParameters"]

is what you are looking for I think.

$\endgroup$
2
  • $\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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.