%This program demonstrates how to use 'for' loops and graphs. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Program description: % This program displays the addition of sine functions % to approximate a square wave. It calculates the % fourier series approximation with an increasing number % of terms. It starts with one sine, then 2 then 3 etc, % until reaching 26 sines in the approximation. % % This program needs no input to run. % % The output of the program is 25 graphs % 1. fourier series approximation with 1 term. % 2. fourier series approximation with 2 terms. % . % . % . % 26. fourier series approximation with 26 terms. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; t=0:.01:4; x=sin(2*pi*t*.5); for i=1:2:51 x=x+(4*5)/(pi*i)*sin(2*pi*i*t*.5); plot(x) N=(i-1)/2 +1; title([' ' num2str(N) ' terms ']); pause end