figure(1)
L = 1.2;
x = -L*(-1:0.001:0.999);
x0 = -0.3;
D = 0.6;
fex = (x>x0) & (x<x0+D);
f = 0.5*D/L*ones(size(x));
plot(x,f,x,fex)
title('n = 0')
axis([-L L -0.2 1.2])
pause
for n = 1:16
    Cp = 1/(2i*n*pi)*exp(1i*n*pi*x0/L)*(exp(1i*n*pi*D/L)-1);	
    Cm = 1/(-2i*n*pi)*exp(-1i*n*pi*x0/L)*(exp(-1i*n*pi*D/L)-1);	
    f = f + Cp*exp(1i*n*pi*x/L) + Cm*exp(-1i*n*pi*x/L);  
    plot(x,f,x,fex)
    title(['n = ',num2str(n)])
    axis([-L L -0.2 1.2])
    pause
end

figure(2)
x = -pi*(-1:0.001:0.999);
fex = exp(-abs(x));
F = fft(fex);
for n = 0:8
    z = zeros(size(x)); z(1:n+1) = 1; z(end-n+1:end) = 1;
    f = ifft(z.*F);
    plot(x,f,x,fex)
    axis([-pi pi 0 1])
    title(['n = ',num2str(n)])
    pause
end

figure(3)
fex = exp(-(x-0.3).^2).*(sin(2*x) + cos(3*x));
F = fft(fex);
for n = 0:8
    z = zeros(size(x)); z(1:n+1) = 1; z(end-n+1:end) = 1;
    f = ifft(z.*F);
    plot(x,f,x,fex)
    title(['n = ',num2str(n)])
    pause
end

figure(4)
while(1)
fn = input('Function: ');
fex = fn(x);
F = fft(fex);
for n = 0:8
    z = zeros(size(x)); z(1:n+1) = 1; z(end-n+1:end) = 1;
    f = ifft(z.*F);
    plot(x,f,x,fex)
    title(['n = ',num2str(n)])
    pause
end
end
