Even Function Fourier Expansion

44 Views Asked by At

I'm having a bit of trouble completing a homework question of mine. I am supposed to expand an equation using Fourier and then graph said equation using Matlab. And no matter what I do the graph of the original equation and the Fourier equation never match up at all.

The question given is:

Given $f(x)= \begin{cases} L/2, & 0 \le x \le L/2 \\ x - L/2, & L/2 \lt x \le L \end{cases}$

expand $f(x)$ in terms of the functions $\{1, \cos(\frac{n{\pi}x}{L})\}, \; n \in \mathbb{Z^+}, \; \text{on } 0 \le x \le L$.

By the looks of it, it wants me to turn it into an even function and to only calculate $a_0$ and $a_n$.

If I do this the fourier series ends up being the following:

$$F(x)= \frac{3L}{8}+ \frac{2L}{\pi^2}\sum_{n=1}^\infty \frac{1}{n^2}[1-(-1)^{n+1}]\cos(\frac{n{\pi}x}{L})$$

If I put that into Matlab and graph it, it will come out as this: f(x) vs F(x). The blue line is the original equation, while the red line is the Fourier expansion. Here is the Matlab code I'm using. As you can see these are nothing alike.

Can someone smarter than me try to pinpoint my mistake and help me understand?