Question

In: Mechanical Engineering

Fourier Series Approximation Matlab HW1:     You are given a finite step function   x(t)=-1, 0<t<4...

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;

Solutions

Expert Solution

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")  

Related Solutions

Find the Fourier series expansion of the function f(t) = t + 4 , 0 =<...
Find the Fourier series expansion of the function f(t) = t + 4 , 0 =< t < 2pi
find Fourier series of periodic function ?(?) = { −?, −1 ≤ ? ≤ 0 with...
find Fourier series of periodic function ?(?) = { −?, −1 ≤ ? ≤ 0 with the period 2 { ?, 0 ≤ ? ≤ 1
Based on fourier series Q1: how to determine if a signal function x(t) is periodic and...
Based on fourier series Q1: how to determine if a signal function x(t) is periodic and ac. And what happens if there is x(t) = sint + cost + sint? How would we know if ac/periodic? Q2: What is fourier series and fourier coefficients? Q3: What is Fourier Trigonometric Series?
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
Find the Fourier series of the function. c. f(t) = sin(3pit), -1</ t </ 1
Find the Fourier series of the function. c. f(t) = sin(3pit), -1</ t </ 1
Use matlab to plot Taylor approximation for f(x) for x near 0 Given f(x) = exp(x)...
Use matlab to plot Taylor approximation for f(x) for x near 0 Given f(x) = exp(x) Use subpots to plot and its absolute and realative erros for N= 1,2,3 PLease give matlab code for Taylor and explain in detail, Thank you
Find the Fourier cosine and sine series of f(t) = t^2 ,0 < t < π
Find the Fourier cosine and sine series of f(t) = t^2 ,0 < t < π
x"(t)- 4x'(t)+4x(t)=4e^2t ; x(0)= -1, x'(0)= -4
x"(t)- 4x'(t)+4x(t)=4e^2t ; x(0)= -1, x'(0)= -4
Solve the following heat equation using Fourier Series uxx = ut, 0 < x < 1,...
Solve the following heat equation using Fourier Series uxx = ut, 0 < x < 1, t > 0, u(0,t) = 0 = u(1,t), u(x,0) = x/2
Solve the following heat equation using Fourier Series uxx = ut, 0 < x < 1,...
Solve the following heat equation using Fourier Series uxx = ut, 0 < x < 1, t > 0, ux(0,t) = 0 = ux(1,t), u(x,0) = 1 - x2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT