Finding the Fourier series

I am trying to write the Fourier series for the following function

f(x) = \left\{\begin{aligned} &0 && -\pi<x<0\\ &e^{-x} && 0<x<\pi \end{aligned} \right.

I used all the same formulas I always use, but the coefficients I got look a bit strange to me

\begin{aligned} a_0 = \frac{1}{\pi}\int_0^\pi e^{-x}dx = \frac{1-e^{-\pi}}{\pi} \end{aligned}

\begin{aligned} a_n = \frac{1}{\pi}\int_0^\pi e^{-x} \cos(nx)dx = \frac{1}{\pi}\int_0^\pi e^{-x}\frac{e^{inx}+e^{-inx}}{2} = \frac{1-e^{-\pi}(-1)^n}{\pi(1+n^2)} \end{aligned}

\begin{aligned} b_n = \frac{1}{\pi}\int_0^\pi e^{-x} \sin(nx)dx = \frac{1}{\pi}\int_0^\pi e^{-x}\frac{e^{inx}-e^{-inx}}{2i} = \frac{n-ne^{-\pi}(-1)^n}{\pi(1+n^2)} \end{aligned}

...and my final solution for the series is

\begin{aligned} F(x) = \frac{1-e^{-\pi}}{2\pi} + \sum_{n=1}^\infty \frac{1-e^{-\pi}(-1)^n}{\pi(1+n^2)} \cos(nx) + \sum_{n=1}^\infty \frac{n-ne^{-\pi}(-1)^n}{\pi(1+n^2)} \sin(nx) \end{aligned}

I tried plotting $F(x)$ and it looked nothing like my function. My other method of checking the solution is to evaluate $F(x)$ at e.g. $x=0$ and see if I get the right sum, but I had no luck doing that either.

I'd be very grateful if anybody could tell me if there's something wrong with my math.

• What do you mean by that? Doesn't that theorem just tell me that F(0)=1/2 ? – slomzivaca Jul 9 '18 at 1:14
• text of my problem states that this is a 2pi periodic function defined over [-pi,pi] – slomzivaca Jul 9 '18 at 1:20
• There is nothing wrong with your math. You found the correct series. – Math Lover Jul 9 '18 at 1:45

Since I cannot comment, here's the plot of your Fourier transform: Your solution is correct. If you're interested this code was used to calculate the function(in Matlab):

function [ y ] = Untitled2( x)
suma = (1-exp(-pi))./(2 * pi);
for n = 1:1000
suma = suma + cos(n.*x).*(1- exp(-pi).*((-1).^n))./(pi.*(1 + n^2));
suma = suma + sin(n.*x).*(n- n.*exp(-pi).*((-1).^n))./(pi.*(1 + n^2));
end
y = suma;
end
• Just a habit. Corrected, thank you. – user1949350 Jul 9 '18 at 1:51
• Can't believe I pondered over the correct solution for hours lol Thanks for checking! – slomzivaca Jul 9 '18 at 2:04
• Fourier Series, not transform! – Vizag Jul 11 '18 at 17:25

The approach suggested by @user1949350 is correct and will work most of the time but I may have found something. It MUST be changed so that it incorporates the idea of convergence within a particular tolerance level. So I am just gonna take his code and extend it to include that part too.

function [ y ] = abc(x,tol)
suma = (1-exp(-pi))/(2 * pi) * ones(1,length(x));
sumb = Inf(1,length(x));
n = 1;

while(max(abs(sumb-suma)) > tol)
disp(n);
sumb = suma;
suma = suma + cos(n.*x).*(1- exp(-pi).*((-1).^n))./(pi.*(1 + n^2));
suma = suma + sin(n.*x).*(n- n.*exp(-pi).*((-1).^n))./(pi.*(1 + n^2));
n = n+1;
end
y = suma;
end

When I set tol = $10^{-4}$, it took about 3000 iterations to converge to that tolerance. So, 1000 terms is a pretty good approximation for this function, but it doesn't lie within $10^{-4}$ tolerance. The plot of the Fourier series looks pretty much the same:

Cheers!