This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Add-ons / Standard Packages / NumericalMath  /


The Mathematica function Fit does linear least-squares fitting to data. It allows any combination of basis functions. However, it is often the case that you want to do trigonometric fitting and it would be easier if you didn't have to explicitly list the basis functions , , , , , . TrigFit

does this for you.

Fitting trigonometric series to data.

TrigFit uses the function Fourier to quickly find a least-squares trigonometric fit to the data. In doing so it completely avoids casting the problem in terms of an overdetermined linear system and instead directly constructs the solution. It should be pointed out, however, that because TrigFit is based on Fourier it must assume that the data come from equally spaced sample points and that the data cover exactly one period of a periodic function. Because of this restriction it was decided that TrigFit should not accept data in the form of ordered pairs. It thus only accepts a list of numbers, which are assumed to be sampled with spacing where is the period of the function and is the number of elements in data. Note that the first datum corresponds to (default value 0). Because the function in question is assumed to be periodic, the first datum also corresponds to (or if is not specified) and the last datum corresponds to


  • This loads the package.
  • In[1]:= <<NumericalMath`TrigFit`

  • Start with some data. Note that the last datum is given by the , not

  • In[2]:= data = Table[1+2Sin[x]+3Cos[2x]+4 Sin[3x],
    {x, 0, 2Pi-2Pi/7, 2Pi/7}];

  • Find the constant term.
  • In[3]:= TrigFit[data, 0, x]


  • This gives the least-squares fit including only the fundamental frequency. Here we specify the period to be rather than the default of

  • In[4]:= TrigFit[data, 1, {x, L}]


  • We can use Fit to do the fitting, but it is slower and less convenient.
  • In[5]:= Fit[Transpose[{Range[0, 2Pi-2Pi/7, 2Pi/7], data}],
    {1, Cos[x], Sin[x]}, x]


  • This gives the least-squares fit including the first 3 overtones. In this case we have specified that the interval

    is the interval covered by the data.
  • In[6]:= TrigFit[data, 3, {x, x0, x1}]


  • As with Fit, it is often useful to discard the small terms using Chop.
  • In[7]:= Chop[%]