% script to plot Fourier series representation of 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./m/pi/pi; %Fourier coefficients for triangle SineFunctions=sin(pi*m'*x); %the jth row of this matrix is sin(j*pi*x) plot(x,FourierCoef*SineFunctions) %plot the function defined by the Fourier series %line 9 above uses a non-obvious trick to construct an MxN matrix as m'*x, %then take the sine of it, meaning the sine of each element. %A more straightforward set of commands to do this would be %SineFunctions=[]; (empty matrix) %for j=1:M (beginning of a loop, a different j for each pass through) %SineFunctions=[SineFunctions; sin(pi*j*x)]; (append the jth row) %end (end the loop) % .. This loop constructs the matrix row by row