# Matlab: Plotting Fourier-Series

• May 17th 2009, 11:43 AM
cith
Matlab: Plotting Fourier-Series
I'm doing some fourier transforms and I'm wondering how to plot this in Matlab to look at the graphs. I don't know how to plot the function, nor the series.

$f(x)=\Big\{_{ 1, \hspace{0.5cm} 0 \leq x < \pi}^{-1, \hspace{0.3cm} -\pi \leq x < 0}$; $f(x+2\pi)=f(x)$, for all x.

which has the fourier series (if calculated correctly):

$b_n=\frac{2}{n\pi}[1-(-1)^n]\to\frac{4}{\pi}\sum_{m=1}^{\infty}\frac{sin((2m-1)x)}{2m-1}$

I'd like to do a comparison of the Fourier-series and the original function by looking at the graphs. Also, how do you find the Fourier-series and their coefficients, from a function like the one I posted? It'd be great to use Matlab for that.

Any help would be appreciated :)
• May 17th 2009, 03:25 PM
CaptainBlack
Quote:

Originally Posted by cith
I'm doing some fourier transforms and I'm wondering how to plot this in Matlab to look at the graphs. I don't know how to plot the function, nor the series.

$f(x)=\Big\{_{ 1, \hspace{0.5cm} 0 \leq x < \pi}^{-1, \hspace{0.3cm} -\pi \leq x < 0}$; $f(x+2\pi)=f(x)$, for all x.

which has the fourier series (if calculated correctly):

$b_n=\frac{2}{n\pi}[1-(-1)^n]\to\frac{4}{\pi}\sum_{m=1}^{\infty}\frac{sin((2m-1)x)}{2m-1}$

x=0: pi/1000 :2*pi;

f=-1+(x>=0)*(2);

plot(x,f);

CB
• May 18th 2009, 03:34 AM
cith
$\sum_{m=1}^{\infty}\frac{sin((2m-1)x)}{2m-1}$
Is there a way to plot this too?

Thanks :)
• May 18th 2009, 06:17 AM
CaptainBlack
Quote:

Originally Posted by cith
$\sum_{m=1}^{\infty}\frac{sin((2m-1)x)}{2m-1}$
Is there a way to plot this too?

Thanks :)

By that I assume you mean the partial sums, then yes:

Code:

x=0:pi/100:2*pi n=1:5;   [N,X]=meshgrid(n,x);   S=sin((2*N-1).*X)./(2*N-1);   SS=sum(S');   plot(x,SS);
and if you want this on the same plot as the other use "hold on" before the second plot (and "hold off" after the second).

CB