Fourier Series Example Script: Difference between revisions

From Class Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
% This script plots a Fourier Series for a Square Wave
% This script plots a Fourier Series for a Square Wave

clf;
clf;
t=0:.01:10;
t=0:.01:10;
T=2.5
T=2.5
M=50
M=50
sum1=0;
sum1=0;
for m=1:2:M,
for m=1:2:M,
sum1 = sum1+4/m/pi*sin(m*pi/2)*cos(2*pi*m*t/T);
sum1 = sum1+4/m/pi*sin(m*pi/2)*cos(2*pi*m*t/T);
end
end
plot(t,sum1,'b-',t(1:10:end),sum1(1:10:end),'r*')
plot(t,sum1,'b-',t(1:10:end),sum1(1:10:end),'r*')
title('Fourier Series Representation of a Square Wave')
title('Fourier Series Representation of a Square Wave')
xlabel('time (seconds)')
xlabel('time (seconds)')
ylabel('Function')
ylabel('Function')
grid on;
grid on;
axis([0,10,-2,2])
axis([0,10,-2,2])
legend('Five Terms','Five Terms Sampled')
legend('Five Terms','Five Terms Sampled')
print("squarewave.png","-dpng") % Prints the plot to a png file called squarewave.png
print("squarewave.png","-dpng") % Prints the plot to a png file called squarewave.png

Revision as of 13:24, 29 September 2010

% This script plots a Fourier Series for a Square Wave
clf;
t=0:.01:10;
T=2.5
M=50    
sum1=0;
for m=1:2:M,
    sum1 = sum1+4/m/pi*sin(m*pi/2)*cos(2*pi*m*t/T);
end
plot(t,sum1,'b-',t(1:10:end),sum1(1:10:end),'r*')
title('Fourier Series Representation of a Square Wave')
xlabel('time (seconds)')
ylabel('Function')
grid on;
axis([0,10,-2,2])
legend('Five Terms','Five Terms Sampled')
print("squarewave.png","-dpng")  % Prints the plot to a png file called squarewave.png