I've been given the task of computing the first 30 coefficients for the Fourier series of a Gaussian wavepacket given by:
$$ f(x)=exp(\frac{-x^2}{2\sigma^2})cos(kx) $$
for $ -L/2<x<L/2 $.
The Fourier coeffcients are calculated in the normal way using
$$ a_r=\frac{2}{L}\int_{-L/2}^{L/2}f(x)cos(\frac{2\pi rx}{L})dx $$
Since the f(x) isn't periodic, it in order to compute these $ a_r $ then the approximation $$ \int_{-L/2}^{L/2}exp(-a(x+id)^2)dx\approx \sqrt{\frac{\pi}{a}} $$ must be made (assuming that L is sufficiently large). This expression is needed to compute the Fourier coefficients (by writing the cosines in terms of complex exponentials).
Now for the question. When plotting the calculated Fourier series against the actual value, the difference in their values increases a lot as x reaches $ -L/2$ or $ L/2 $. I can't figure why this is. I initially thought it was due to Gibbs phenomena, however, the function is even and hence should not have a discontinuity if repeated. Any help would be greatly appreciated.
You did not specify the parameters $L$, $\sigma$, $k$ for which you observe the strange behavior.
Anyway, the Fourier series performs well on this function; the error you observe can only be attributed to wrong coefficients. Likely due to the approximation $$\int_{-L/2}^{L/2}\exp(-a(x+id)^2)\,dx\approx \sqrt{\frac{\pi}{a}}$$ which is fine when $L\gg d$, but it breaks down badly when $d$ is sizable compared to $L$. For example, the values of $\int_{-10}^{10} \exp(-(x+id)^2)\,dx$ for $d=0,\dots, 8$ are