%==========================================================================
%MMG400, Task 6.1
% Date: 2016-10-10
% Written by: Johannes Borgqvist
% The program calculates the Fourier series
% of the function f(x)=x by using five
% terms in the sum, it calculates the
% errror between the series and the actual
% function and it plots the result in a nice
% figure
%==========================================================================
%% CALCULATE THE FOURIER SERIES OF 
% FIFTH ORDER
%==========================================================================
% Number of terms in the sum
nuOfTerms = 5;
% Allocate memory for the coefficients in the sum
coefficient = zeros(nuOfTerms,1);
% Calculate the coefficients in the sum
for n = 1:nuOfTerms
    coefficient(n,1) = ((-2*((-1)^n))/(n));
end
% Define number of elements
nuOfEle = 500;
% Define the upper limit for the calculation
upperLimit = 2*pi;
% Definie a vector from zero to upper element
% with ''nuOfEle'' elements
x = linspace(0,upperLimit,nuOfEle)';
% Allocate memory for the fourier
% series
sum5 = zeros(nuOfEle,1);
% Loop through all x-values
for i = 1:nuOfEle
    % Loop through all number of terms
    % for the given x-value
   for n = 1:nuOfTerms 
       sum5(i,1) = sum5(i,1) + (coefficient(n,1)*sin(n*x(i,1)));
   end
end
%==========================================================================
%% Draw a line at x=pi
%==========================================================================
% Define verctor marking where pi is
piVec = pi*ones(nuOfEle+1,1);
% Define lower limit for the pi-vecor
lowLimit = -4;
% Give the y-values from ''lowLimit'' to 
% upper limit
yVal(1) = lowLimit;
for index = 2:(nuOfEle+1)
   yVal(index) = yVal(index-1) + ((upperLimit-lowLimit)/(nuOfEle)); 
end
%==========================================================================
%% CALCULATE THE ERROR
%==========================================================================
% Calculate the square of the coefficients
coeffSquare = coefficient'*coefficient;
% Calculate the first term
firstTerm = ((2*pi*pi)/(3));
% Calculate the error
error = firstTerm-coeffSquare;
%==========================================================================
%% PLOTTING RESULTS NICELY
%==========================================================================
% Lots of fancy command to make the plot look nice
fig = figure('units','normalized','outerposition',[0 0 1 1],...
    'PaperPositionMode','auto');
clf
% DELPLOT 1
subplot(1,2,1)
p1 = plot(x,x,'red',x,sum5,'blue');
set(p1,'Linewidth',2);
hold on 
p2 = plot(piVec,yVal,'--black');
set(p2,'Linewidth',4);
grid on 
leg1 = legend('$f(x)=x$','$S_{5}(x)$','$x=\pi$');
set(leg1,'Fontsize',20,'interpreter','latex','location','NOrthWest');
axis([0,2*pi,-pi-0.05,2*pi+0.05])
ax1 = gca;
set(ax1,'Fontsize',30,'Ticklabelinterpreter','latex');
set(ax1,'Xtick',[0*pi; 0.5*pi; pi; 1.5*pi; 2*pi]);
set(ax1,'Xticklabel',{'$0$';'$\frac{\pi}{2}$';...
    '$\pi$'; '$\frac{3\pi}{2}$'; '$2\pi$'});
set(ax1,'Ytick',[-pi; -0.5*pi;0*pi; 0.5*pi; pi; 1.5*pi; 2*pi]);
set(ax1,'Yticklabel',{'$-\pi$';'$-\frac{\pi}{2}$' ;'$0$'; '$\frac{\pi}{2}$';...
    '$\pi$'; '$\frac{3\pi}{2}$'; '$2\pi$'});
% DELPLOT 2
subplot(1,2,2)
p1 = plot(x,x,'red',x,sum5,'blue');
set(p1,'Linewidth',2)
hold on 
grid on 
axis([0,pi,0,pi+0.05])
leg2 = legend('$f(x)=x$','$S_{5}(x)$');
set(leg2,'Fontsize',20,'interpreter','latex','location','NorthWest');
ax2 = gca;
set(ax2,'Fontsize',30,'Ticklabelinterpreter','latex');
set(ax2,'Xtick',[0*pi; 0.25*pi; 0.5*pi;...
    0.75*pi; pi; 1.25*pi; 1.5*pi; 1.75*pi; 2*pi]);
set(ax2,'Xticklabel',{'$0$';...
    '$\frac{\pi}{4}$' ;'$\frac{\pi}{2}$'; '$\frac{3\pi}{4}$';...
    '$\pi$'; '$\frac{5\pi}{4}$';...
    '$\frac{3\pi}{2}$'; '$\frac{7\pi}{4}$'; '$2\pi$'});
set(ax2,'Ytick',[-pi; -0.5*pi;0*pi;...
    0.25*pi; 0.5*pi; 0.75*pi;pi; 1.5*pi; 2*pi]);
set(ax2,'Yticklabel',{'$-\pi$';'$-\frac{\pi}{2}$'...
    ;'$0$'; '$\frac{\pi}{4}$';'$\frac{\pi}{2}$';...
    '$\frac{3\pi}{4}$';'$\pi$';...
    '$\frac{3\pi}{2}$'; '$2\pi$'});
annotation('textbox', [0 0.9 1 0.1], ...
    'String', ...
    ['\texttt{MMG400, UPPGIFT 6.1}:',...
    '\textbf{Fourierserien av funktionen $f(x)=x$ av femte ordningen}',...
    '\textbf{$\;\textrm{\fbox{$S_{5}(x)=\sum_{n=1}^{5}',...
    '\left(\frac{2\cdot(-1)^{n+1}}{n}\right)\cdot\mathrm{sin}(nx)$}}$}'], ...
    'EdgeColor', 'none', 'interpreter','latex','Fontsize',20, ...
    'HorizontalAlignment', 'center')
annotation('textbox', [0 0 1 0.06], ...
    'String', '\textbf{Variabelv\"{a}rde}$\;\;x$', ...
    'EdgeColor', 'none', 'interpreter','latex','Fontsize',40, ...
    'HorizontalAlignment', 'center')
t1 = text(-4.7,0.6,'\textbf{Funktionsv\"{a}rde$\;\;f(x)$}');
set(t1,'Rotation',90,'Fontsize',40,'interpreter','latex')
t2 = text(-2.5,1.9,{['$\mathrm{Fel}^{2}=\|f(x)-S_{5}(x)\|^{2}=$',num2str(error)],...
    '',...
    ['$\;\;\;$d\"{a}r$\;\;\langle f(x),S_{5}(x)\rangle',...
    ' =\frac{1}{2\pi}\int_{-\pi}^{\pi}\;',...
    '\left(f(x)\cdot S_{5}(x)\right)\mathrm{d}x$']});
set(t2,'interpreter','latex','Fontsize',22);
print(fig, '-depsc', '-r300', 'MMG400_LinAlg2_Task6_1_20161010.eps')
%print(fig, '-dpng', '-r300', 'MMG400_LinAlg2_Task6_1_20161010.png')