Sawtooth2 Matlab Code
<source lang="matlab">
%
% _-_,, ,, _-_- ,,
% ( // || /, _ ; || '
% _|| /'\\ ||/\\ \\/\\ || __ < \, \\/\/\ ||/\ \\ \\/\\ _-_,
% _|| || || || || || || ~||- - /-|| || | | ||_< || || || ||_.
% || || || || || || || ||===|| (( || || | | || | || || || ~ ||
% -__-, \\,/ \\ |/ \\ \\ ( \_, | \/\\ \\/\\/ \\,\ \\ \\ \\ ,-_-
% _/ `
%
% John Hawkins
% LNA
% Sawtooth Wave Fourier Example
% 12 Jan 2010
t=linspace(-1,1,1000);
x=t-floor(t);
figure
plot(t,x,'k')
hold on;
f=1/2*ones(1,numel(t));
for n=1:100
Plus=j/(2*pi*n)*exp(j*2*pi*n*t);
Minus=j/(2*pi*-n)*exp(j*2*pi*-n*t);
f=f+Plus+Minus;
plot(t,Plus+Minus+1/2);
switch n
case 1
f1=f;
case 3
f3=f;
case 5
f5=f;
case 10
f10=f;
case 50
f50=f;
end
end
plot(t,f,'r');
axis equal;
fontsize=20;
title({['Sawtooth Wave Constructed of First 100 Terms'],...
['of Fourier Series, Each Term Shown']},...
'fontname','times','fontsize',fontsize)
xlabel('t'); ylabel('x');
figure
plot(t,f-x)
title('Error when x(t) is Truncated after 100 Terms',...
'fontname','times','fontsize',fontsize)
xlabel('Error'); ylabel('t');
figure
plot(t,f1,t,f3,t,f5,t,f10,t,f50,t,f)
axis equal
title('Sawtooth Wave Constructed of First n Terms',...
'fontname','times','fontsize',fontsize);
xlabel('t'); ylabel('x');
legend('n=1','3','5','10','50','100','location','northwest');
</source>