Matlab - The Fourier Series

In summary, the function Fourier series calculates the sum of two harmonic waves, each with a frequency corresponding to an integer value.
  • #1
18
0
I have some problems on 'for' and 'while' loop so I could not write a MATLAB program for this question;
Gdq_Clipboard01.jpg
.

Well, how can I start?
 
Physics news on Phys.org
  • #2
First of all, I assume you are writing a .m script file i.e. you start off with:
Code:
function y = f(x)

%%The loop would look like:

y=0.0;

for n = 1:50
  
   y = y + 2(-1)^(n+1) ( (pi^2)/n - (6/(n^3)) sin(n x);

end

%%After which you could write

x = 1.0 : 0.1 : 2*pi
y = f(x);
plot(y,x)
 
  • #3
Oh, I got the point! Thanks for replying..
 
  • #4
Hi, I am trying to plot a Fourier series in MATLAB for the function

x(t)

= 0 when -pi < wt < 0
= (4/pi)*wt when 0 < wt < pi/2
= 2 when pi < wt < 2*pi

I know that a0 = 3/2, an = (1/pi)*(4/(pi*i^2))*(cos((pi*i/2)-1)), and bn = (1/pi)*((4/(pi*i^2))*(sin(pi*i/2-1))-2/pi*(cos(pi*i)))

so far I have the code as:

N = 11;
x = [0:100]/100;
f = zeros(1,101)*1/2;
for i = 1:2:N
a = (1/pi)*(4/(pi*i^2))*(cos((pi*i/2)-1));
b = (1/pi)*((4/(pi*i^2))*(sin(pi*i/2-1))-2/pi*(cos(pi*i)));
f = f + a*cos(i*x)+ b*sin(i*x);
end
f = .75 + f;

plot(x,f); any help?? I tried tweaking the window so that it would plot the entire period, but it's not coming out correctly. any help??
 
  • #5
Hi, this is the code I did for my first assignment on Fourier series. It might be considered "cheating" in terms of programming since I used the Matlab's symbolic method to do all the grunt work. I figured if Matlab offered such a nice/easy feature then I might as well use it, lol :P

For a square wave:
[tex]V(x)=\left\{\begin{matrix} 1 & 0<x<\frac{L}{2}\\ -1 & \frac{L}{2}<x<L \end{matrix}\right.[/tex]

Fourier Series
[tex]V_{N}(x)=\sum_{n=0}^{N}a_{n}\sqrt{\frac{2}{L}}cos(\frac{n2\pi x}{L}) + \sum_{n=1}^{N}b_{n}\sqrt{\frac{2}{L}}sin(\frac{n2\pi x}{L})[/tex]

where
[tex]a_{n} = \frac{2L}{n\pi}\sqrt{\frac{2}{L}}sin^2(\frac{n\pi}{2})sin(n\pi)[/tex]

[tex]b_{n} = -\frac{2L}{n\pi}\sqrt{\frac{2}{L}}sin^2(\frac{n\pi}{2})cos(n\pi)[/tex]

One can change the summation limit N in the code below by changing the value 20, the last parameter in the function

V_x = symsum(..., n, 1, 20);

Code:
L = 8;
syms n x;

a_n = sqrt(2/L)*( (2*L)/(n*pi) )*( (sin((n*pi)/2))^2 )*sin(n*pi); %fourier coefficient a_n
b_n = -sqrt(2/L)*( (2*L)/(n*pi) )*( (sin((n*pi)/2))^2 )*cos(n*pi); %fourier coefficient b_n
x = 0:0.2:8; %range
V_x = symsum( a_n*sqrt(2/L)*cos( (n*2*pi*x)/L ) + b_n*sqrt(2/L)*sin( (n*2*pi*x)/L ), n, 1, 9 ); %fourier series

plot(x, V_x);
grid on;
axis([ 0 8 -1.4 1.4]);
hold on;
 
Last edited:

1. What is the Fourier Series in Matlab?

The Fourier Series in Matlab is a mathematical tool used to represent a periodic function as a sum of sine and cosine functions. It is based on the principle that any periodic function can be represented as an infinite sum of sinusoidal functions with different amplitudes, frequencies, and phases.

2. How do you calculate the Fourier Series in Matlab?

To calculate the Fourier Series in Matlab, you first need to define the periodic function and its period. Then, you can use the built-in function fft to compute the coefficients of the Fourier Series. These coefficients can then be used to plot the Fourier Series in the time or frequency domain.

3. What is the difference between the Fourier Series and the Fourier Transform in Matlab?

The Fourier Series in Matlab is used to represent a periodic function, while the Fourier Transform is used to represent a non-periodic function. The Fourier Transform also provides a continuous representation in the frequency domain, while the Fourier Series is a discrete representation in the frequency domain.

4. How can the Fourier Series be used in signal processing?

The Fourier Series is a powerful tool in signal processing as it allows us to analyze and manipulate signals in the frequency domain. This can be useful for filtering out unwanted frequencies, detecting periodicity in a signal, and extracting important features from a signal.

5. Are there any limitations to using the Fourier Series in Matlab?

One limitation of using the Fourier Series in Matlab is that it assumes the input signal is periodic, which is not always the case in real-world applications. Additionally, the accuracy of the Fourier Series is limited by the number of terms used in the summation. Using a larger number of terms may result in longer computation times and may not always provide significant improvements in accuracy.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
651
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top