# Fourier series on MATLAB

#### zkrhrmli

Joined May 6, 2018
12
Hi.

I need a bit of help. I want to make a repetitive y=x^2 graph. I am think by using fourier method which is setting the limit and repeat it.

y=-x^2; 0<x<pi;
y=x^2; pi<x<2pi

But i could not plot it on MATLAB.

Do anyone have any suggestion on it?

#### bogosort

Joined Sep 24, 2011
696
I'm not sure what you're asking. If all you want is to plot the periodic extension of y = x^2, you don't need a Fourier series -- you can plot it as a piecewise function, e.g.,:

$$f(x) = \begin{cases} \, & \vdots \\ (x+8)^2 &, \: -10 < x < -6 \\ (x+4)^2 &, \: -6 < x < -2 \\ x^2 &, \: -2 < x < 2 \\ (x-4)^2 &, \: 2 < x < 6 \\ (x-8)^2 &, \: 6 < x < 10 \, & \vdots \\ \end{cases}$$

That's the periodic extension of x^2 with period 4, which looks like: A simple way to plot piecewise functions in MATLAB is to plot each interval separately, using the 'hold' command between plot calls. Recent versions include a 'piecewise' command that lets you define the function symbolically.

If instead you want to plot the truncated Fourier series, you'll need to calculate the Fourier coefficients of the periodic extension. In this particular case it's just a sum of cosines, as f(x) is an even function:

\begin{align} F(x) &= \frac{4}{3} - \frac{16}{\pi^2} \sum_{k=1}^\infty \frac{(-1)^k}{k^2} cos(\frac{k\pi x}{2}) &= \frac{4}{3} - \frac{16}{\pi^2} \left( cos(\frac{\pi}{2} x) - \frac{cos(\pi x)}{2^2} + \frac{cos(\frac{3\pi}{2}x)}{3^2} - \frac{cos(2\pi x)}{4^2} + \cdots \right) \end{align}

Of course, the finite series can only approach f(x), but it is straightforward to plot in MATLAB.

#### Attachments

• zkrhrmli

#### zkrhrmli

Joined May 6, 2018
12
yess, i am trying to plot a periodic extension of y=x^2.

sir, is this my server that cant read your text or this is exactly how it is? i am sorry, but what does "truncated Fourier series" means?

i am new with fourier series and matlab.

#### bogosort

Joined Sep 24, 2011
696
yess, i am trying to plot a periodic extension of y=x^2.

sir, is this my server that cant read your text or this is exactly how it is? i am sorry, but what does "truncated Fourier series" means?

i am new with fourier series and matlab.
Unfortunate that the LaTeX didn't render for you. Here's an image of how it looks for me: A truncated Fourier series is a finite sum. Since it is not practical to use the infinite sum of a complete Fourier series, we truncate the series, i.e., we pick a "stopping point". The Fourier series converges to the function only when we include all the terms, so any truncated series can at best approximate the function; the more terms in the series, the smaller the error between the function and its approximation. Try plotting F(x) above in MATLAB, first with 2 or 3 terms; as you add more terms, you'll notice the convergence.

But if you're only interested in plotting the periodic extension of x^2, then you can use the piecewise f(x) in the image above, which will be much more accurate than the Fourier series approximation F(x).

#### John_2016

Joined Nov 23, 2016
55
Hi there

MATLAB has a command called repmat (REPeat MATrix) precisely to do what you are asking for

try this

% base signal
dx=pi/100;
x1=[-pi:dx: pi];
y1=x1.^2;

% train
T=10; % amount times to repeat
x=repmat(x1,1,T) % reference signal
y=repmat(y1,1,T) % train

now you can plot

plot(x,y)

does it help?

John BG

Last edited:

#### zkrhrmli

Joined May 6, 2018
12
Hi there

MATLAB has a command called repmat (REPeat MATrix) precisely to do what you are asking for

try this

% base signal
dx=pi/100;
x1=[-pi:dx: pi];
y1=x1.^2;

% train
T=10; % amount times to repeat
x=repmat(x1,1,T) % reference signal
y=repmat(x1,1,T) % train

now you can plot

plot(x,y)

does it help?

John BG

why (x1,1,T) ? why do we have to plot 1?

#### John_2016

Joined Nov 23, 2016
55
you are right, it's actually

x=repmat(x1,1,T) % reference signal
y=repmat(y1,1,T) % train