![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/391273/image.jpeg)
How to Plot a Fourier Series?
608 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alexander McGlone
le 24 Oct 2020
Réponse apportée : Gabriele Bunkheila
le 29 Juil 2024
Hi,
I was given a half range expansion function f(x) = 0, 0<x<2 and f(x) = 1, <2<x<4.
I was asked to solve for the Fourier cosine series and Fourier sine series and then plot each.
I solved by hand:
Fourier Cosine Series:
a0 = 1/2
an = -2/(n*pi)*sin((n*pi)/2)
f(x) = 1/2 + sum(an*cos((n*pi*x)/4)) from n =1 to infinity
Fourier Sine Series:
bn = [2/(n*pi)]*[(-1)^(n+1) + cos((n*pi)/2)]
f(x) = sum(bn*sin((n*pi*x)/4))
I'm fairly new to Matlab and very unexperienced, where I'm having dificulty is plotting these functions against x, say x = [-24 24] and n=1:1:50 or until square waves appear. I gained some experience plotting their partial sums using fplot, but that approach does not appear to work here. It isn't feasible to plot the 50th partial sum. I've seen more arrayfun usage online, but I do not understand it whatsoever.
Any help and explanation would be greatly appreciated. Thanks in advance.
0 commentaires
Réponse acceptée
Alan Stevens
le 24 Oct 2020
Modifié(e) : Alan Stevens
le 24 Oct 2020
Here's the cosine version. You can add the sin version and play with different ranges for n:
x = -24:0.1:24;
n = 10;
ycos = fcos(x,n);
plot(x,ycos),grid
xlabel('x'),ylabel('cos function')
% Define functions
function f = fcos(x,n)
f = zeros(1,numel(x));
f = 1/2;
for i = 1:n
an = -2*sin(i*pi/2)/(i*pi);
f = f + an*cos(i*pi*x/4);
end
end
This results in
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/391273/image.jpeg)
Plus de réponses (1)
Gabriele Bunkheila
le 29 Juil 2024
If you need a review of the basics on Fourier Analysis, I recommend taking a look at the Fourier Analysis learning module on MATLAB Central File Exchange (including the Sine and Cosine series App captured below).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1742271/image.png)
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!