# Writing a Fourier Series Equation

I am trying to program a Fourier Series equation that I will use for a normed fit that is not a least squares fit. If I write

f[t_]:= a[[1]]/2 + Sum[a[[2 k]] Sin[2 k π t] + a[[2 k + 1]] Cos[2 k π t], {k, 1, kMax}]


the equation compiles. However, I cannot use it later. I believe the problem is that I have not defined the coefficients correctly.

Can anyone tell me how to define this equation? I want to create a list of the function at points on an interval for which I have other data to be fitted, so I can create an error function and minimize for the best fit coefficients. I am trying to create the function with unknown coefficients, so I can use a range of norm functions.

• You need to define your function so that it is dependent on both the list a and kMax, as well as t. Commented Oct 6, 2016 at 3:20

You could define a function for a[n] and b[n] and apply the definition of Fourier series. Here is a quick example

Suppose you want to find F.S. of this function

ClearAll[t, n, f];
T0 = 1;(*period*)
f[t_] := Piecewise[{{t, 0 < t < 1/2}, {1/2, 1/2 < t < T0}, {0, True}}];
Plot[f[t], {t, 0, T0}, PlotLabel -> "f(t)"]


Then

an[n_] = If[n == 0, 1/(T0) Integrate[f[t], {t, 0, T0}],
2/T0 Integrate[f[t] Cos[n 2 Pi/T0 t], {t, 0, T0}]];
bn[n_] = 2/T0 Integrate[f[t] Sin[n 2 Pi/T0 t], {t, 0, T0}];
f[t_, max_] :=
an[0] + Sum[
an[n] Cos[n 2 Pi/T0 t] + bn[n] Sin[n 2 Pi/T0 t], {n, 1, max}];


Now plot it for 20 terms

nTerms = 20;
Show[Plot[f[t], {t, 0, T0}, PlotStyle -> {Red}],
Plot[Evaluate@f[t, nTerms], {t, -T0, T0}], PlotRange -> All]


or 50 terms for better approximation

nTerms = 40;
Show[Plot[f[t], {t, 0, T0}, PlotStyle -> {Red}],
Plot[Evaluate@f[t, nTerms], {t, -T0, T0}], PlotRange -> All]


btw, Fourier series support is already build-in Mathematica, so you do not have to implement it, and it has better performance than the above, which becomes slow for large number of terms.

• Thanks for responding. Regrettably, I'm not trying to do standard Fourier series. I am working on an interval that is not a direct overlap of the data range, and I have re-derived the theory slightly to modify for the mapping. In addition, I will not use a least squares fit because I need a robust set of norms.
– JimT
Commented Oct 7, 2016 at 16:31
• The result is that I am not able to use the standard capabilities built into Mathematica which give the standard derivation of the coefficients and use least squares norms when fitting. Unfortunately, I am still learning how to build functions correctly. I manually derived a version of my equations and programmed the matrix solutions into Mathematica, but the matrices are large and a little unstable, hence my desire to move to alternative norms. Now I need to learn how to program the series expansion in an arbitrary form.
– JimT
Commented Oct 7, 2016 at 16:31
• Still getting used to function definition in Mathematica, so any help on an generalized equation form would be great. Thanks.
– JimT
Commented Oct 7, 2016 at 16:31