In: Mechanical Engineering
Fourier Series Approximation Matlab HW1:
You are given a finite step function
x(t)=-1, 0<t<4
1, 4<t<8
.
Hand calculate the FS coefficients of x(t) by assuming
half- range expansion, for each case below and modify the
code.
Approximate x(t) by cosine series only (This is
even-half range expansion). Modify the below code and plot the
approximation showing its steps changing by included number of FS
terms in the approximation.
Approximate x(t) by sine series only (This is odd-half
range expansion).. Modify the below code and plot the approximation
showing its steps changing by included number of FS terms in the
approximation.
You are given a code below which belongs to a
different function, if you run this code it works and you will see
it belongs to ft=1 0<t<2 0 2<t<4 . In this code f(t) is
only approximated by even coefficients (cosine series).
Upload to BB learn using the Matlab HW1 link:
A multi page pdf file that shows
The hand calculations of FS coefficients
The original function plotted
The approximated functions plotted for b and
c.
A Comment on how FS expansion approximates
discontinuities in the function.
MATLAB CODE
_____________________________________________________________________________________________________________________________________________________________________
clear all
close all
% Example MATLAB M-file that plots a Fourier series
% Set up some input values
P = 4; %period = 2P
num_terms = 100; %approximate infinite series with finite number of
terms
% Miscellaneous setup stuff
format compact; % Gets rid of extra lines in output.
Optional.
% Initialize x-axis values from -1.25L to 1.25L. Middle number is
step size. Make
% middle number smaller for smoother plots
t = -4:0.001:4;
x=zeros(length(t),1); % reseting original half range
expanded function array
x(0<=t & t<2)=1; %
forming the original half range expanded function array for the
purpose of plotting only
x(2<t & t <4)=0;
x(t<0 & -2<t)=1;
x(t<-2 & t>-4)=0;
figure %plotting original half range
expanded function
plot(t,x)
axis([-4.5 4.5 -0.5 1.5])
%Starting to approximate f(t)
% Initialize y-axis values. y = f(t)
f = zeros(size(x'));
% Add a0/2 to series
a0 = 1;
f = f + a0/2;
% Loop num_terms times through Fourier series, accumulating in
f.
figure
for n = 1:num_terms
% Formula for an.
an = (2/(n*pi))*sin(n*pi/2);
bn=0;
% Add cosine and sine into f
f = f + an*cos(n*pi*t/P) +
bn*sin(n*pi*t/P);
% Plot intermediate f. You can comment
these three lines out for faster
% execution speed. The function pause(n)
will pause for about n
% seconds. You can raise or lower for
faster plots.
plot(t,f);
set(gca,'FontSize',16);
title(['Number of terms = ',num2str(n)]);
grid on;
if n < 5
pause(0.15);
else
pause(0.1);
end
xlabel('even approx');
end;
Screenshot of the code: t=0 (0<y<4)
Screenshot of the output :
Code to copy :
clf;
t=0:.01:4;
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:4:end),sum1(1:4: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')
% Prints the plot to a png file called squarewave.png
print("squarewave.png","-dpng")
Screenshot of the code: t = 1 (4 < y < 8)
Screenshot of the output :
Code to copy :
clf;
t=1:.04:8;
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(4:8:end),sum1(4:8: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')
% Prints the plot to a png file called squarewave.png
print("squarewave.png","-dpng")