Exploring Fejer's Theorem and the equidistribution of n alpha mod 1

This notebook explores the Fejer and Fourier Series approximations to (an approximation of) the characteristic function of the interval [1/3, 2/3]; this is one of the key ingredients in our analysis of the equidistribution of n alpha mod 1. We first define the characteristic function and the continuous analogues, then define the Fejer and Fourier series approximations. We conclude with some plots (which can be made into movies.

charfn[x_] := If[x ≥ 1/3 && x ≤ 2/3, 1, 0]; (* this is the characteristic function of the interval [1/3, 2/3] *);
A2[x_,j_] := If[x ≥ 1/3 && x ≤ 2/3, 1, If[x ≥ 1/3 - 1/j && x ≤ 1/3, j*(x - 1/3 + 1/j), If[x ≥ 2/3 && x ≤ 2/3 + 1/j,1- j*(x - 2/3), 0]]];
(* this is the continuous function A2 which equals the characteristic function on the interval [1/3, 2/3], drops linearly to 0 at 1/3 - 1/j and 2/3 + 1/j, and is zero everywhere else. *)

(* here are plots of our two functions *)
Plot[charfn[x], {x,0,1}]
Plot[A2[x,10], {x,0,1}]

(* we now compute the mth Fourier coefficient of A2[x,j] *)
fouriercoeff[m_,j_] := NIntegrate[ A2[x,j] *Cos[2 Pi m x], {x,0,1}]
fouriercoeff[0,1000]
(* as a check, we look at the zeroth Fourier coefficient when j is large. The zeroth coefficient is the area under the function A2; for large j this is basically 1/3. *)
(* we should use Exp[2 Pi m x] and not Cos[2 Pi I m x]; however, as the function is symmetric about the point x = 1/2, all the sin terms vanish *)
(* NOTE: we use NIntegrate and not Integrate to save computer time. It is possible to work with exact formulas, but these take more time and would impact the movies later. *)

(* We now define the two key procedures. From a computational point of view, it is idiotic to recompute the Fourier coefficients each time. Thus, given a j and an M, we compute the first M Fourier coefficients of A2, and save that in a list to be used in construction the Fejer adn Fourier series approximations to A2. *)

(* this procedure computes the first M Fourier coefficients of A2[x,j] *)
makefouriercoeff[M_, j_] := Module[{},
fouriercoefflist = {};
For[m = 0,m ≤ M,m++, fouriercoefflist = AppendTo[fouriercoefflist, fouriercoeff[m,j]]];
];

(* this procedure calls the previous to compute the first M Fourier coefficients of A2[x,j]. It then builds the Fourier and Fejer approximations with m in {-M, ..., M}. We have to be a little careful in how Mathematica saves entries (the important fact to remember is the first element of a list is denoted 1, so our zeroth Fourier coefficient corresponds to the first element in the list. *)
makeFourierFejerfn[M_, j_] := Module[{},
makefouriercoeff[M,j];
templist = fouriercoefflist; (* templist is shorter to write! *)
(* the next line constructs the Fourier series for m in {-tempM, ..., tempM}. By symmetry the sin terms vanish, and everything is just a sum of cosines. We choose to do things up to tempM and not M to give us freedom to animate / create movies later *)
F[x_,tempM_] := templist[[1]] + Sum[2 * templist[[m+1]] Cos[2 Pi m x], {m,1,tempM}]; 
(* the next line constructs the Fejer series for m in {-tempM, ..., tempM}. *)
T[x_,tempM_] := templist[[1]] + Sum[2 * ((tempM-m)/tempM) * templist[[m+1]] Cos[2 Pi m x], {m,1,tempM}]; 
(* the next few lines print our results in such a way that we can create movies in M. In other words, we can vary M and see how the approximations improve. *)
Print[Manipulate[Plot[F[x,Floor[tempM]], {x,0,1}], {tempM,1,M}]];
Print["This is a plot of the Fourier approximation to A2 when (M,j) = (", M, ",", j, ")."];
Print[Manipulate[Plot[T[x, Floor[tempM]], {x,0,1}], {tempM, 1, M}]];
Print["This is a plot of the Fourier approximation to A2 when (M,j) = (", M, ",", j, ")."];
Print[Manipulate[Plot[A2[x,j], {x,0,1}], {tempM,1,M}]];
Print["This is A2 when j = ", j, "."];
Print[Manipulate[Plot[F[x,Floor[tempM]] - A2[x,j], {x,0,1}, PlotRange -> {-.6, .6}], {tempM,1,M}]];
Print["Plot of the Fourier approximation minus A2."];
];

(* this line generates the pictures / movies *)
makeFourierFejerfn[100,400]