function problem10()
clear
clc
clf
hold on

%fourierDemo();

x = 0:0.01:pi;
%plot(x,f2(x));
for n = 5:10
fourierApproximation(2^n);
end

fprintf('Done\n');

end

function fourierDemo()

x = 0:2*pi/100:2*pi;

for m = 0:5
    plot(x,sin(m*x),'color',[rand() rand() rand()]);
end

end

function fourierApproximation(Np)



for m = 0:5
 %   plot(x,sin(m*x),'color',[rand() rand() rand()]);
end
%plot(x,f1(x))
x = 0:2*pi/1000:1*pi;
dx = 1*pi/(Np+1);
xi = dx:dx:1*pi-dx;
IM = zeros(Np,Np);
b = zeros(Np,1);
for i = 1:Np
    for j = 1:Np
        IM(i,j) = sin(j*xi(i));
    end
    b(i) = f2(xi(i));
end
an = inv(IM)*b;
uh = zeros(size(x));
for m = 1:Np
    uh = uh + an(m)*sin(m*x);
end

%plot(x,uh,'color',[rand() rand() rand()])
plot(x,log10(abs(uh-f1(x))),'color',[rand() rand() rand()])
end


function dy = f1(x)
dy = exp(-(x-pi/2).^2*4) - exp(-(0-pi/2).^2*4);
end

function dy = f2(x)
dy = (abs(x-pi/2)<pi/6);
end