3

EDIT:

I'm aware I could just add a compensating "$-c_1(0)$" term to get rid of the vertical offset, but that feels not in the spirit of Fourier series... and doesn't explain the mystery anyway.

Into Desmos, I plotted:

$$f(x)=x^k\\c_1(n)=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\cos(nx)\,dx\\c_2(n)=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\sin(nx)\,dx\\\mathcal{F}(x)=\sum_{n=0}^Nc_1(n)\cdot\cos(nx)+c_2(n)\cdot\sin(nx)$$

The $c_1$ and $c_2$ functions are coefficient-finding functions, and rely on Desmos' numerical integration.

And this Fourier series $\mathcal{F}(x)$ fits $f(x)$ very very well, with more accuracy as $N$ increases, only when $k$ is odd. When $k$ is an even integer, the series fits the curve very well still... but at a large y-offset. The series oscillates around some y-intercept, whose value as a function of $\pi$ I have not been able to experimentally determine. For reference, here are two images, the first of $k$ odd, the second of $k$ even. The green lines are the original function, and the red lines are the Fourier series.

$k=3$ and the Fourier series fits well: k=3 $k=4$ and the series fails... bizarre vertical offset. k=4 failure

Many thanks for any suggestions about where this comes from, and how to correct it.

FShrike
  • 40,125
  • 6
    Remember that $c_1(0)$ is special—it has an extra factor of $\frac12$ in its formula. (Imagine $f(x)$ is constant and see that this is necessary.) That would explain being off by a constant when the mean is nonzero, as a vertical offset indicates. PS: one doesn't need numerical integration to calcluate the $c_j(n)$: integration by parts gives recursive formulas for them. – Greg Martin Jul 20 '21 at 21:27
  • 1
    The reason the odd ones are correct is that $x^k$ is then odd and the $c_0$ term is zero. The error Greg Martin noted does not occur for odd $k$. – Ross Millikan Jul 20 '21 at 21:29
  • @GregMartin I am rather new to Fourier series... when you say "an extra factor", where does it go? And is this unique to this particular Fourier series, or for all? – FShrike Jul 20 '21 at 21:29
  • @GregMartin Well it is sorted now... I guessed where it should go and got it right. However, I am mystified as to why $c_1(0)$ must be halved even for non-constant $f$... – FShrike Jul 20 '21 at 21:34
  • @GregMartin Nevermind - I understand it now. Thank you – FShrike Jul 20 '21 at 21:39
  • 1
    You also should not have a $c_2(0)$ at all. – Ross Millikan Jul 20 '21 at 21:46
  • @RossMillikan I believe it just automatically zeros itself out with using the way of writing the formulae that I have – FShrike Jul 20 '21 at 21:47
  • That is correct as $\sin(0)=0$ – Ross Millikan Jul 20 '21 at 21:52

1 Answers1

2

Answer for any student who may or may not view this later, with credit to @GregMartin: the first coefficient in a Fourier series, when using this method of deriving the coefficients, must be halved: $$c_1(n)=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\cos(nx)\,dx,n\gt0\\c_1(0)=\frac{1}{2\pi}\int_{-\pi}^{\pi}f(x)\,dx$$

This is because in the derivation of the coefficient formulae for cosine, we have $$c_1(n)=\frac{\langle f(x),\cos(nx)\rangle}{\langle\cos(nx),\cos(nx)\rangle}$$

But $$\langle\cos(0\cdot x),\cos(0\cdot x)\rangle=2\pi$$

(but is equal to $\pi$ for all other $n$). The braces denote the inner product calculated over the range $[-\pi,\pi]$.

FShrike
  • 40,125