Fourier Series part wise function.

I have a part wise function which im trying to plot with its Fourier series. This is my code, since my Fourier has coefficients I have to find them and then do the sum but im not getting the plot im supposed to. What am I doing wrong? NOTE: the first loop its for me to plot the original function.
J = 100; x = linspace(-pi,pi,J); M = 20;
for k = 1:length(x)
if (x(k) >= -pi && x(k) <= 0)
f(k) = (1 + (x(k)/pi));
elseif (x(k) >= 0 && x(k) < 0.5*pi)
f(k) = (1 - (2*x(k)/pi));
else (x(k) > 0.5*pi && x(k) <= pi);
f(k) = (-1 + (x(k)/pi));
end
end
for n = 1:M
A_0 = 5/8;
A_n = 1/n/pi*(sin((n*pi)/2)/2+3/n/pi*(1-cos((n*pi)/2)));
B_n = -1/n/pi*(cos((n*pi)/2)/2)+(3/n/pi)*sin((n*pi)/2);
FS = A_0/2 + A_n*cos(n*x) + B_n*sin(n*x);
end
plot(x,FS,'r')
hold on
plot(x,f)
Thanks in Advance.

Réponses (0)

Catégories

En savoir plus sur Fourier Analysis and Filtering dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by