2
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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!

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – anderstood Sep 9 '15 at 15:06
  • 1
    $\begingroup$ Look at the documentation "guide/StandardExtraPackages" $\endgroup$ – user31001 Sep 9 '15 at 15:19
  • $\begingroup$ @anderstood With "NFourierSeries" you can get warning messages. Please insert then "AccuracyGoal". $\endgroup$ – user31001 Sep 9 '15 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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