stepsize = 0.1; t = [-pi:stepsize:pi]; f = t.*t - 2; % function to approximate N = 3; %Number of fourier coefficients n = [-N:N]; %of course, the -nth fourier cosine coefficient is the same as the nth, but the 0th gets a weight of half, so this evens things out (even if it slows things down, but if we were optimizing we'd be using the fft anyway!) A = (stepsize)*[(1/sqrt(pi))*cos(n'*t)*f']; f_N = (1/2)*A'*(1/sqrt(pi))*cos(n'*t);