This question already has an answer here:

I have two arrays contain $y$ and $k$,respectively. However, the exact function relationship is not clear. What I what to do is to fit $y(k)$ by Fourier series

$\qquad y=\sum_n c_n \cos(nk)$

up to $n>20$ terms and obtain their coefficients $c_n$. Can anyone help me? I would be very grateful for your kindness.


marked as duplicate by Nasser, LCarvalho, m_goldberg, J. M. will be back soon Oct 23 '17 at 13:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Duplicate of this, I believe. $\endgroup$ – bobthechemist Oct 23 '17 at 12:47
  • $\begingroup$ Thanks,but when I try to fit my data, there is fitted curves but no fitting parameters. Meanwhile, an error raised. ' Fit::fitm: Unable to solve for the fit parameters; the design matrix is nonrectangular, non-numerical, or could not be inverted.' why is that? $\endgroup$ – mxguan Oct 25 '17 at 3:40
  • $\begingroup$ It is not possible to answer your questions without more information about the data and the code used to fit the data. My first guess is that you are not using the NonlinearModelFit function correctly, and need to look at the documentation and the referenced Stack Exchange questions for examples. $\endgroup$ – bobthechemist Oct 25 '17 at 12:54