%function that plots the result of summing the first N terms of the Fourier %series expansion for a function fx function[] = plotfourier(N) dx = .001; L = 2; x = -L:dx:L; Nx = length(x); fx = x/L; %actual function, here just linear from -1 to 1 a = zeros(1,N); b = zeros(1,N); a0 = (dx/L)*sum(fx); fest = (a0/2)*ones(1,Nx); for n=1:N b(n) = (dx/L)*sum(sin(n*pi*x/L).*fx); a(n) = (dx/L)*sum(cos(n*pi*x/L).*fx); fest = fest + b(n)*sin(n*pi*x/L); fest = fest + a(n)*cos(n*pi*x/L); end plot(x,fx,'r'); hold on; plot(x,fest,'k'); legend('Actual','Estimate'); xlabel('x'); ylabel('f'); end