I am trying to fit a Fourier cosine series to discrete data. I read through this, but I was not able to understand it. The data should be periodic with a period of $0.2 \text{ m}$, and it is the initial temperature distribution of a copper rod. I will then be using this function to solve the heat equation. Any ideas on how to compute a Fourier cosine series that fits this data (without the error bars)?

data = 
  {{0, 26.75}, {0.01, 26.5}, {0.02, 28}, {0.03, 28.75}, {0.04, 30}, {0.05, 32}, 
   {0.06, 34.25}, {0.07, 37.5}, {0.08, 43.25}, {0.09, 59.5}, {0.1, 102}, 
   {0.11, 67.75}, {0.12, 40.5}, {0.13, 35.75}, {0.14, 34}, {0.15, 31.75}, 
   {0.16, 30}, {0.17, 27.5}, {0.18, 28}, {0.19, 26.5}, {0.2, 28}}


Data including error bars

  • $\begingroup$ Why do you expect a Cosine series when the data is not an even function? Fourier is the function to use to get the series from numerical data. However unless the data is even you will get a complex set of Fourier coefficients. $\endgroup$
    – Hugh
    Jul 5, 2015 at 6:56
  • $\begingroup$ I don't really mind, and the data is equally spaced 1cm apart. I need a trigonometric series because when solving the PDE it seems to work when I use one. But I think any normal Fourier series should work. Any ideas on how to make one with this data? $\endgroup$ Jul 5, 2015 at 6:57
  • 1
    $\begingroup$ I am away from my computer at the moment but will look at this later. As an alternative have you considered interpolation? $\endgroup$
    – Hugh
    Jul 5, 2015 at 9:57

1 Answer 1


Once upon a time, one of the Standard Packages bundled with Mathematica was the package NumericalMath`TrigFit`​. As the package has now been deprecated, I have taken it upon myself to slightly clean up the implementation inside the package. Here it is:

trigFit[data_?VectorQ, n_Integer, {x_, x0_: 0, x1_}] :=
Module[{c0, clist, cof, k, m, t}, 
    m = Min[n, Quotient[Length[data] - 1, 2]];
    cof = If[! VectorQ[data, InexactNumberQ], N[data], data];
    clist = Rest[cof]/2;
    cof = Prepend[{1, I}.{{1, 1}, {1, -1}}.{clist, Reverse[clist]}, First[cof]];
    cof = Fourier[cof, FourierParameters -> {-1, 1}];
    c0 = First[cof]; clist = Rest[cof];
    cof = Chop[Take[{{1, 1}, {-1, 1}}.{clist, Reverse[clist]}, 2, m]];
    t = Rescale[x, {x0, x1}, {0, 2 π}];
    c0 + Total[MapThread[Dot, {cof, Transpose[
                               Table[{Cos[k t], Sin[k t]}, {k, m}]]}]]]

Applied to your data:

n = 9; (* order of fit, adjust as needed *)
f[x_] = trigFit[data[[All, 2]], n, {x, Min[data[[All, 1]]], Max[data[[All, 1]]]}]

Plot[f[x], {x, 0, 0.2},
     Epilog -> {Directive[AbsolutePointSize[4], Red], Point[dat]},
     Frame -> True, PlotRange -> All]

Fourier fit

  • $\begingroup$ Hi thank you so much for this. But when I tried to use this function to solve the heat equation I'm getting a really wierd function. I've tried using different functions to solve the PDE, but for some reason Mathematica only outputs a reasonable function when I use a Fourier Cosine series. Is there any way I could represent this as as a sum of Cosines? $\endgroup$ Jul 5, 2015 at 13:25
  • $\begingroup$ I think your problem is a bit deeper than just finding a cosine series. Why not ask a new question about your PDE, and then link to this question of yours for context? $\endgroup$ Jul 5, 2015 at 13:43

Your Answer

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

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