clear; clf(); lambda=10; %Wavelength k=2*pi/lambda; %Wave number x=0:0.01:40; %plot range c0 = 1/2;%Y axis offset f_x= c0/2; % c is a vector representing the coefficients %c = 2/pi*[1, 0, -1/3, 0, 1/5, 0, -1/7]; %Square wave %s = 2/pi*[0, 0, 0, 0, 0, 0, 0]; c = 2/pi*[0, 0, 0, 0, 0, 0, 0]; s = 2/pi*[-1, 1/2, -1/3, 1/4, -1/5, 1/6, -1/7]; %Sawtooth wave max = numel(c); %determines the number of coefficients we have in the vector for i=1:max fc = cos(i*k*x); %creates a vector of cos(ikx) fs = sin(i*k*x); %creates a vector of sin(ikx) f_x = f_x + c(i)*fc + s(i)*fs; %this makes the total f vector end %can also build using a matrix method in the for loop: %fmat(i,:) = f; %creates a matrix storing all vectors %then use: %f_x= c0/2 + c(1)*fmat(1,:) + c(2)*fmat(2,:) + c(3)*fmat(3,:) + c(4)*fmat(4,:); plot(x, f_x);