I have the following discrete data (6001 data points) stored in the external file called analysis: the format of data set as:

-0.32026    -0.00185
-0.32016    0.00488
-0.32005    0.01161
-0.31994    0.01834
-0.31983    0.02507
-0.31973    0.0318
-0.31962    0.03853
-0.31951    0.04526
-0.31941    0.05199

what I want is to fit the data by Fourier series, and get fitting parameters $C_n$ (n>20)

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

code likes the following:

list = Import["C:\Users\mxguan\Desktop\analysis.txt", "Table"]

bothfit = Fit[list, Flatten[Table[{Cos[n*x], Sin[n*x]}, {n, 0, end}]], x];

bothfit = 
Fit[list, Flatten[Table[{Cos[n*x], Sin[n*x]}, {n, 0, end}]], x];

ListPlot[list, PlotRange -> All, Joined -> True, 
PlotStyle -> Directive[Red, Thickness[0.015]]],

Plot[bothfit, {x, list[[1, 1]], list[[-1, 1]]}, 
PlotRange -> All,PlotStyle -> Black]}, PlotRange -> {-10, 10}, 
ImageSize -> 350, PlotLabel -> 
"Both Fit: n = " <> "0.." <> ToString[end]]
, {end, {70}}]

however,during the calculation,there are some error raiesd:

Table::iterb: Iterator {n,0,end} does not have appropriate bounds.

General::stop: Further output of Table::iterb will be suppressed during this calculation.

Fit::fitm: Unable to solve for the fit parameters; the design matrix is nonrectangular, non-numerical, or could not be inverted.

The result is that only fitting curve are plot, but I can not find the fitting parameter,is there any fault in a input?

enter image description here

  • $\begingroup$ What is the value of end supposed to be? $\endgroup$ Oct 25, 2017 at 7:22
  • $\begingroup$ @J.M. Actually I'm a new learner for Mathematica and the code is based on the former question. link . I thank end is the maximum value of series number n. $\endgroup$
    – mxguan
    Oct 25, 2017 at 7:34
  • 1
    $\begingroup$ Since you didn't specify a value of end, you're getting these errors. $\endgroup$ Oct 25, 2017 at 8:04
  • $\begingroup$ @J.M I think end have been set as 70 in the above example. Do you have any idea how can I output the fitting parameters? $\endgroup$
    – mxguan
    Oct 25, 2017 at 9:14
  • 1
    $\begingroup$ Then there should be an end = 70; line before the one for bothfit. On another note: the function seems to be odd, so why are you fitting with cosine instead of sine? $\endgroup$ Oct 25, 2017 at 9:17


Your Answer

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

Browse other questions tagged or ask your own question.