Fourier Approximations for simple x^n functions on [-1, 1]
https://en.wikipedia.org/wiki/Fourier_series
%Lots of thanks too:
%https://www3.nd.edu/~nancy/Math30650/Matlab/Demos/fourier_series/fourier_series.html
%******Magic Fourier Solver******
syms k L n x
evalin(symengine,'assume(k,Type::Integer)');
%A_n
a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);
%B_n
b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);
%nth partial sum
fs = @(f,x,n,L) a(f,x,0,L)/2 + ...
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n);
%******FUNCTIONS(s) GO HERE*******
f1 = x;
%******Plotter******
figure
p1 = ezplot(fs(f1,x,1,1));
hold on
p2 = ezplot(fs(f1,x,2,1));
p3 = ezplot(fs(f1,x,3,1));
p4 = ezplot(fs(f1,x,4,1));
p5 = ezplot(fs(f1,x,5,1));
p6 = ezplot(fs(f1,x,6,1));
p7 = ezplot(fs(f1,x,7,1));
p8 = ezplot(fs(f1,x,8,1));
p9 = ezplot(fs(f1,x,9,1));
p10 = ezplot(fs(f1,x,100,1));
p11 = ezplot(fs(f1,x,1000,1));
%Graph Customization
set(p1, 'LineWidth', 1.2, 'Color',[0.8,0,0.2])
set(p2, 'LineWidth', 1.2, 'Color',[1,0,0])
set(p3, 'LineWidth', 1.2, 'Color',[1,0.5,0])
set(p4, 'LineWidth', 1.2, 'Color',[1,1,0])
set(p5, 'LineWidth', 1.2, 'Color',[0,1,0])
set(p6, 'LineWidth', 1.2, 'Color',[0,0.6,0.2])
set(p7, 'LineWidth', 1.2, 'Color',[0,0.8,0.8])
set(p8, 'LineWidth', 1.2, 'Color',[0,0,1])
set(p9, 'LineWidth', 1.2, 'Color',[0.2,0,0.8])
set(p10, 'LineWidth', 1.2, 'Color',[0.5,0,0.5])
set(p11, 'LineWidth', 1.2, 'Color',[0,0,0])
axis([-1 1 -1.5 1.5])
grid on
legend('n=1', 'n=2', 'n=3', 'n=4', 'n=5', 'n=6', 'n=7', 'n=8', 'n=9', 'n=100', 'n=1000', 'Location', 'best')
xlabel('x');
ylabel('y');
title({'Fourier Approximations for f(x) = (Function goes here)'});