function [ft] = fexpand(N) % Fourier Expansion of abs(x) on (-pi,pi) % Output: function ft = Nth partial sum of Fourier Series % Input: N = Number of terms to take in summation % Define interval of interest: x = [-pi:0.01:pi]; % Zero output function: ft = zeros(1,length(x)); %Set a0 term: a0 = pi/2; ft = a0; % Compute each 'a' and 'b' and sum: for n = 1:N a = 2*((-1)^(n)-1)/(pi*n^2); b = 0; ft = ft + a*cos(n*x) + b*sin(n*x); end % All Done! return