0% found this document useful (0 votes)
29 views13 pages

LAB - Fourier Series

The document provides MATLAB commands and examples for calculating Fourier series and their coefficients for periodic functions. It includes specific commands for computing cosine and sine coefficients, as well as plotting the partial sums of the Fourier series for various functions, such as f(x) = |x| and f(x) = x. Additionally, it discusses the Gibbs Phenomenon and provides an example of a square wave function represented by a Fourier sine series.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

LAB - Fourier Series

The document provides MATLAB commands and examples for calculating Fourier series and their coefficients for periodic functions. It includes specific commands for computing cosine and sine coefficients, as well as plotting the partial sums of the Fourier series for various functions, such as f(x) = |x| and f(x) = x. Additionally, it discusses the Gibbs Phenomenon and provides an example of a square wave function represented by a Fourier sine series.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Work on the Matlab Commands in conjunction with the worked out examples given

below.

Fourier Series
The Fourier series of a periodic function is given by

where the Fourier coefficients and are given by

and

The nth partial sum of the Fourier series is

You can use the following commands to calculate the nth partial sum of the Fourier
series of the expression f on the interval [-L,L]
syms x k L n

The next command tells MATLAB that k is an integer. That will


allow simple and simplify to evaluate and for a symbolic integer k.
evalin(symengine,'assume(k,Type::Integer)');

The kth Fourier cosine coefficient of f is given by the command:


a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);

The kth Fourier sine coefficient is given by the command:


b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);

The nth partial sum is given by


fs = @(f,x,n,L) a(f,x,0,L)/2 + ...
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n);

For example, To calculate the Fourier series of f(x) = |x| on the interval [-1,1].
f = abs(x)

f =

abs(x)

The 10th partial sum is


pretty(fs(f,x,10,1))

We can also have MATLAB calculate the general Fourier coefficients. To do this and
get MATLAB to simplify the results, we can use simple (Note: Some Matlab versions
don’t have simple commands, so you can also try simplify). The following command
gives the kth Fourier cosine coefficient, suppressing the results of all of the steps
of simple except for the simplest.
[A,how]=simple(a(f,x,k,1))

A =

-(4*sin((pi*k)/2)^2)/(pi^2*k^2)

how =

simplify

If you don't want to see how simple found the answer, you can suppress the output,
then just display the simplified answer. The following command does that for the kth
Fourier sine coefficient.
[B,how]=simple(b(f,x,k,1)); B

B =

0
Here are the plots of the partial sums for n=2, 5, 10. The plot also shows the function
f.
ezplot(fs(f,x,2,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=2')

ezplot(fs(f,x,5,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=5')
ezplot(fs(f,x,10,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=10')

Now I do it with the function f(x) = x on [-1,1].


f = x

f =

The Fourier cosine coefficients are


[A,how]=simple(a(f,x,k,1)); A

A =

The Fourier sine coefficients are


[B,how]=simple(b(f,x,k,1)); B

B =

-(2*(-1)^k)/(pi*k)

The 10th partial sum is


pretty(fs(f,x,10,1))
Here are plots of the partial sums for n=2, 5,10,20,50.
ezplot(fs(f,x,2,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=2')

ezplot(fs(f,x,5,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=5')
ezplot(fs(f,x,10,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=10')

ezplot(fs(f,x,20,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=20')
ezplot(fs(f,x,50,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=50')

If you zoom in here, you notice that the graph of the partial sum of the Fourier series
looks very jagged. That is because ezplot does not plot enough points compared to the
number of oscillations in the functions in the partial sum. We can fix this problem
using plot. This also allows us to use different colors for the plot of the original
function and the plot of the partial sum. To use plot, we need to turn the partial sum
into an inline vectorized function and specify the points where it will be evaluated.
g = inline(vectorize(fs(f,x,50,1)));
X = -1:.001:1;
plot(X,g(X),'r')
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=50')
g = inline(vectorize(fs(f,x,100,1)));
X = -1:.0001:1;
plot(X,g(X),'r')
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=100')
Notice that no matter how many terms we take, the partial sum always overshoots the
function x at the end points of the interval. If we do our two plots on the interval [-2,2],
we see that outside [-1,1], the partial sum doesn't have much to do with the function.
ezplot(fs(f,x,20,1),-2,2)
hold on
ezplot(f,-2,2)
hold off

A Fourier series on [-L,L] is 2L periodic, and so are all its partial sums. So, what we are
really doing when we compute the Fourier series of a function f on the interval [-L,L] is
computing the Fourier series of the 2L periodic extension of f. To do that in MATLAB,
we have to make use of the unit step function u(x), which is 0 if and 1 if . It is
known as the Heaviside function, and the MATLAB command for it is heaviside. In
MATLAB, heaviside(0)=1/2. The function h(x) = u(x-a)u(b-x) is 1 on the interval [a,b]
and 0 outside the interval.
f = heaviside(x+3)*heaviside(-1-x)*(x+2) + heaviside(x+1)*heaviside(1-x)*x
...
+ heaviside(x-1)*heaviside(3-x)*(x-2);

extends f(x) = x to be periodic on [-3,3], with period 2. To check that we've extended
it correctly, we plot it.
ezplot(f,-3,3)
title('Periodic extension of x')

Here is a plot of the function and its Fourier series.


ezplot(fs(x,x,20,1),-3,3)
hold on
ezplot(f,-3,3)
hold off
title('Periodic extension of x and partial sum with n=20')
It is somewhat clearer if the plots aren't in the same color, so We use plot for the
partial sum.
X = -3:0.001:3;
g = inline(vectorize(fs(f,x,20,1)));
ezplot(f,-3,3)
hold on
plot(X,g(X),'r')
hold off
title('Periodic extension of x and partial sum with n=20')

The overshoots are at the discontinuities of the 2 periodic extension of f. This is called
the Gibbs Phenomenon.
Example (Square Wave): Consider the following square wave
function defined by the relation
 1 , 0  x  0.5
f ( x)  
 1 , 0.5  x  1

This function is shown below.

f(x)

1.0

x
0.5 1.0

-1.0

We will assume it has an odd periodic extension and thus is representable by a


Fourier Sine series

nx 1 L nx
f ( x)   bn sin bn   L f ( x) sin dx , n  1,2,3,
n 1 L , L L

where L = 1/2. Evaluating the Fourier coefficients gives

bn  21 / 2 f ( x) sin 2nxdx  21 / 2 (1) sin 2nxdx  20 (1) sin 2nxdx 
1/ 2 0 1/ 2 2
1  (1) n 
n
and thus the series representation reduces to

4
f ( x)   n sin 2nx
n 1, 3, 5 ,...
The evaluation of this series can easily be done using MATLAB and a code is given
in the text box below. This code loops and evaluates the partial sums of the series
and plots them until an error criteria is satisfied. A couple of the plots are shown
below

% Fourier Series Evaluation & Plotting Example


% Square Wave Function
% Sum & Plot FS Partial Sums Until Error Level Is Reached
% *******************************************************
clc;clear all;clf;
x=linspace(0,1.5,1000);
f=0;
for n=1:2:200
% First Plot Square Wave Function
X=[0,0,0.5,0.5,1.0,1.0,1.5,1.5];
Y=[0,1,1,-1,-1,1,1,0];
line(X,Y,'color','r','linewidth',2)
grid on;hold on;
f=f+(4/(n*pi))*sin(2*pi*n*x);
error=mean((abs(f)-1).^2);
plot(x,f,'k','linewidth',2)
title(['Square Wave FS Partial Sum: ',...
'n = ',num2str(n),' Error = ',num2str(error)])
pause
if error<0.01
break
end
clf
end

Square Wave FS Partial Sum: n = 5 Error = 0.067914 Square Wave FS Partial Sum: n = 21 Error = 0.019523
1.5 1.5

1 1

0.5 0.5

0 0

-0.5 -0.5

-1 -1

-1.5 -1.5
0 0.5 1 1.5 0 0.5 1 1.5

You might also like