% script to plot Fourier series representation of % the derivative of the triangle function % M is number of Fourier terms % N is number of x values in [0,1] % M and N must be supplied from outside m=1:M; %m labels the Fourier terms x=linspace(0,1,N); %x is the independent variable FourierCoef=4*sin(m*pi/2)./m/pi; %Fourier coefficients for diff(triangle) CosFunctions=cos(pi*m'*x); %the jth row of this matrix is cos(j*pi*x) plot(x,FourierCoef*CosFunctions) %plot the function defined by the Fourier series %line 10 above uses a non-obvious trick to construct an MxN matrix as m'*x, %then take the cosine of it, meaning the sine of each element. %A more straightforward set of commands to do this would be %CosFunctions=[]; (empty matrix) %for j=1:M (beginning of a loop, a different j for each pass through) %CosFunctions=[CosFunctions; cos(pi*j*x)]; (append the jth row) %end (end the loop) % .. This loop constructs the matrix row by row