# NIntegrate option for FourierSeries

I would like to use FourierSeries but it is very slow. My guess is that it is because it uses symbolic integration:

(* function for the example *)
f[t_] = Cos[t] + 0.2*Sin[2*t] + Piecewise[{{.1, 1 < Mod[t, 2 Pi] < 2}}, 0];
(* using FourierSeries *)
FourierSeries[f[t], t, 3]; // AbsoluteTiming
(* using NIntegrate, manually *)
Total[Table[Exp[I*k*t]/(2 Pi)*NIntegrate[f[t]*Exp[-I*k*t],{t, -Pi, Pi}],
{k, -3, 3}]]; // AbsoluteTiming


10.129225 (* for FourierSeries *)

1.923198 (* with NIntegrate *)

I am sure there is an easy way to force FourierSeries to use NIntegrate, but I don't know how.

Why don't you use NFourierSeries?

<< FourierSeries
f[t_] = Cos[t] + 0.2*Sin[2*t] + Piecewise[{{.1, 1 < Mod[t, 2 Pi] < 2}}, 0];
NFourierSeries[f[t], t, 3]; // AbsoluteTiming
{3.39228, Null}


This time is measured on my laptop!

• For a very simple reason: I did not know NFourierSeries existed. That's exactly what I was looking for. Computation time is very close to that of NIntegrate (tested for k from 3 to 20), but it's simpler. I did not find NFourierSeries from the documentation. I think it is because it requires the package FourierSeries. What would have been a good approach to possibly find it? – anderstood Sep 9 '15 at 15:06
• Look at the documentation "guide/StandardExtraPackages" – user31001 Sep 9 '15 at 15:19
• @anderstood With "NFourierSeries" you can get warning messages. Please insert then "AccuracyGoal". – user31001 Sep 9 '15 at 16:03