Plotting Fourier Series Expansion

I was given a problem of the Fourier series expansion of with a periodicity interval . I found that the Fourier series expansion of such function with given periodicity interval is . However, I am new to MATLAB and my knowledge is limited to simple plotting. Suppose that my answer is correct, how to subplot and its Fourier series expansion using loop that generates the n-order of the expansion? I searched in this community and actually find a really good plotting, but I don't understand how it works. Thanks in advance!

2 Comments

Here, I am attaching the complete works on how do I find the Fourier series expansion just in case it would help anyone who want to help me to make the subplot (see in problem 1).
I also want to share my attempt on this:
x = linspace(-10,10,100);
y = 1-(4*cos(x))/pi - (4*cos(3*x))/(9*pi) - (4*cos(5*x))/(25*pi);
plot(x,y,'r-');
Of course, it is very simple because I am new to MATLAB.

Sign in to comment.

 Accepted Answer

Hi Hanif,
Maybe this will help.
First, the problem statement says that one period spans -1 < x <= 1 (I don't think it matter if using < or <=)
x = linspace(-1,1,100);
Instead of using your problem, let's do something simpler, like 1 - 2*sum(n=1:5,n*x)
fsum = 0*x; % initialize
for n = 1:5
fsum = fsum + n*x;
end
plot(x,1-2*fsum)
I think you can adapt that to your problem. Of course we can't compute an infinite sum, so you'll have to pick an upper bound for the sum over n.

3 Comments

Hi Paul,
Thank you for your help.
This actually a very good example to understand the basic for a MATLAB newcomer like me. I have tried my problem on your program.
x = linspace(-10,10,100);
fsum = 4*cos((pi*x)/(pi^2)); % initialize
for n = 2:10
fsum = fsum + 4*cos((2*n-1)*pi*x)/(pi^2*(2*n-1)^2);
end
y = 1-fsum;
plot(x,y,'r-')
The result was good. However, is there a way to improve this program such that it plots the Fourier series expansion of for each n, i.e., for , for , and so on? Let's say I want to stop at , so I have five graphs in one plot.
Use plot inside the loop, with hold set to on. Also, the initial value for fsum was incorrect. And still need to define x over the correct interval
% x = linspace(-10,10,100); should be from -1 to 1
% fsum = 4*cos((pi*x)/(pi^2)); % initialize, should be 4*cos(pi*x)/pi^2;
% for n = 2:10
% fsum = fsum + 4*cos((2*n-1)*pi*x)/(pi^2*(2*n-1)^2);
% end
% y = 1-fsum;
% plot(x,y,'r-')
x = linspace(-1,1,100); % changed this line
fsum = 0*x; % initialize
figure;
hold on;
for n = 1:10 % start loop at 1
fsum = fsum + 4*cos((2*n-1)*pi*x)/(pi^2*(2*n-1)^2);
plot(x,1-fsum)
end
The sum seems like it's biased up by 0.5. Also, as your code showed, the reconstruction is not periodic with period = 2. Recheck derivation?
I see. I actually tried to put the plot function into the loop, but it did not work because I forgot to put the hold-on command. As you also mentioned, I haven't check my derivation again, but I will do! Thank you for your helps, Paul.

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 9 Mar 2023

Commented:

on 9 Mar 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!