Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Construct a least-squares trigonometric fit to data
| ResourceFunction["TrigFit"][data,n,x] gives the least‐squares trigonometric fit to data up to cos(nx) and sin(nx), with fundamental period 2π. | |
| ResourceFunction["TrigFit"][data,n,{x,L}] gives the least‐squares trigonometric fit to data up to cos(2πnx/L) and sin(2πnx/L), with fundamental period L. | |
| ResourceFunction["TrigFit"][data,n,{x,x0,x1}] gives the least‐squares trigonometric fit to data up to cos(2πn(x-x0)/(x1-x0)) and sin(2πn(x-x0)/(x1-x0)), with fundamental period x1-x0. | 
Generate data corresponding to one period of a periodic function:
| In[1]:= | ![data = Table[
   Sin[x]/(5/4 - Cos[x]) + Cos[x]/(5/4 + Sin[x]), {x, 0, 2 \[Pi] - 2 \[Pi]/11, 2 \[Pi]/11}];](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/7ab391c0b1d26c62.png) | 
Construct a low-order trigonometric fit:
| In[2]:= | ![tf[x_] = ResourceFunction["TrigFit"][data, 4, x]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/0512a526588cfa5f.png) | 
| Out[2]= |  | 
Show the fit along with the original data:
| In[3]:= | ![Show[Plot[tf[x], {x, 0, 2 \[Pi]}], ListPlot[data, DataRange -> {0, 2 \[Pi] - 2 \[Pi]/11}, PlotStyle -> ColorData[97, 4]]]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/51c703d6a16da296.png) | 
| Out[3]= |  | 
Generate samples from a periodic function:
| In[4]:= | ![dat = Table[{1, Cos[x], Cos[2 x], Sin[x], Sin[2 x]}, {x, 0, 2 \[Pi] - 2 \[Pi]/7, 2 \[Pi]/7}] . RandomVariate[NormalDistribution[], 5]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/58addc43197d1f88.png) | 
| Out[4]= |  | 
Trigonometric fit over [0,2π]:
| In[5]:= | ![ResourceFunction["TrigFit"][dat, 1, x]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/746362b6a3437ff0.png) | 
| Out[5]= |  | 
Trigonometric fit over [0,ℒ]:
| In[6]:= | ![ResourceFunction["TrigFit"][dat, 1, {x, 0, \[ScriptCapitalL]}]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/575c1cd384cc1638.png) | 
| Out[6]= |  | 
Trigonometric fit over [x0,x1]:
| In[7]:= | ![ResourceFunction[
 "TrigFit"][dat, 1, {x, Subscript[x, 0], Subscript[x, 1]}]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/574cc01fb8cda683.png) | 
| Out[7]= |  | 
Sample points over a closed curve:
| In[8]:= | ![dat = Table[
  N[{JacobiP[5, 1, 2, Cos[t]], JacobiP[5, 1, 2, Sin[t]]}], {t, 0, 2 \[Pi] - \[Pi]/25, \[Pi]/25}]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/674d60cc3bea6fad.png) | 
| Out[8]= |  | 
Plot the trigonometric fit along with the original data:
| In[9]:= | ![ParametricPlot[
 ResourceFunction["TrigFit"][dat, 3, {t, 2 \[Pi]}] // Evaluate, {t, 0,
   2 \[Pi]}, Epilog -> {Directive[AbsolutePointSize[4], ColorData[97, 2]], Point[dat]}]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/46ecfd23ed29327c.png) | 
| Out[9]= |  | 
Generate samples from a periodic function:
| In[10]:= | ![dat = Table[{1, Cos[x], Cos[2 x], Cos[3 x], Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 \[Pi] - 2 \[Pi]/11, 2 \[Pi]/11}] . RandomVariate[NormalDistribution[], 7]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/3ff3515dc8ebb15c.png) | 
| Out[10]= |  | 
Use TrigFit to construct the trigonometric fit:
| In[11]:= | ![ResourceFunction["TrigFit"][dat, 3, {x, 2 \[Pi]}]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/0bb991baaa96ffa2.png) | 
| Out[11]= |  | 
Use Fit to construct the trigonometric fit:
| In[12]:= | ![Fit[Transpose[{Table[x, {x, 0, 2 \[Pi] - 2 \[Pi]/11, 2 \[Pi]/11}], dat}], Prepend[Flatten[Table[{Cos[k x], Sin[k x]}, {k, 3}]], 1], x]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/2352bceafdafaf90.png) | 
| Out[12]= |  | 
Sample points on a knot:
| In[13]:= | ![knotf8 = Table[
   KnotData["FigureEight", "SpaceCurve"][x], {x, 0, 2 \[Pi] - \[Pi]/32, \[Pi]/32}];](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/56e419d0be625b52.png) | 
Construct a low-order trigonometric fit from the data:
| In[14]:= | ![f8trig[t_] = ResourceFunction["TrigFit"][knotf8, 6, t]](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/7f0672bcdd75acab.png) | 
| Out[14]= |  | 
Plot the trigonometric fit of the knot, and compare with the result of KnotData:
| In[15]:= | ![{ParametricPlot3D[f8trig[t], {t, 0, 2 \[Pi]}, Axes -> False, Boxed -> False, Mesh -> None, PlotRange -> All, PlotStyle -> Tube[0.2, Method -> {"Caps" -> False}], ViewPoint -> {0, 0.01, 5}], KnotData["FigureEight"]} // GraphicsRow](https://www.wolframcloud.com/obj/resourcesystem/images/847/8475605c-8b58-40c7-83ff-46afcfc3ec5f/2581737a44ca860c.png) | 
| Out[15]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License