# Integrating problem using interface

lately I tried to develop an interface that will plot the furier sum of a given function up to a given number of terms.

The code is here:

isEven[f_] := SameQ[f[t], f[(-t)]]
a[n_, f_] := If[isEven[f], Integrate[f[y]*Cos[n y], {y, -Pi, Pi}], 0]
b[n_, f_] := If[isEven[f], 0, Integrate[f[y]*Sin[n y], {y, -Pi, Pi}]]
fourier[f_, m_] := Sum[a[n, f]*Cos[n x] + b[n, f]*Sin[n x], {n, 1, m}]

Interpretation[
{g = x^2, min = 0, max = 2 Pi, sum = 5},
Panel[
Grid[{{Style["Furier Series", Bold], SpanFromLeft},
{"Function:", InputField[Dynamic[g]]},
{"Min:", InputField[Dynamic[min]]},
{"Max:", InputField[Dynamic[max]]},
{"Sum:", InputField[Dynamic[sum]]}}]
],
Plot[Evaluate[fourier[g[x], sum]], {x, min, max}]]


The first part specifies the functions I would need, and the second part is an inteface to use the tool.

When i run the code, a keep getting these errors: And then gives me an empty plot.

Does anyone here know what is the problem?

Thanks a lot!

• First of all, please revise your theory about Fourier Analysis. In a hurry, you integrate wrt y and your function is defined as dependent on x... – José Antonio Díaz Navas Dec 2 '17 at 14:08

After some corrections, and removing some parts not needed (the code is equally fast), I suggest this (with NIntegrate the code goes faster):

a[n_, f_] := 1/\[Pi] Integrate[f*Cos[n x], {x, -Pi, Pi}];
b[n_, f_] := 1/\[Pi] Integrate[f*Sin[n x], {x, -Pi, Pi}];
fourier[f_, m_] := 1/(2 \[Pi]) Integrate[f, {x, -Pi, Pi}] +
Sum[a[n, f]*Cos[n x] + b[n, f]*Sin[n x], {n, 1, m}];

Interpretation[{g = x^2, min = -Pi, max = Pi, sum = 5},
Panel[Grid[{{Style["Fourier Series", Bold],
SpanFromLeft}, {"Function:", InputField[Dynamic[g]]}, {"Min:",
InputField[Dynamic[min]]}, {"Max:",
InputField[Dynamic[max]]}, {"Sum:", InputField[Dynamic[sum]]}}]],
Plot[{g, Evaluate[fourier[g, sum]]}, {x, min, max},
PlotRange -> All, Frame -> True, PlotLegends -> {"f(x)", "Fourier Approx."}]]  