You are on page 1of 2

%%% Coeficinetes de Fourier

clear all, close all, clc


syms A0 Ak Bk Ck dt fn k t T0 W0 Y % declaring variables
%DEFINE FUNCTION
% fn=t;
%FIND FOURIER COEF's
W0=1; %define W0=1 fundamental frequency
T0=(2*pi); %Period of the function
AK=(2/T0)*int(t*cos(k*t*W0),t,-(T0/2),(T0/2));
BK=((2/T0)*int(t*(sin(k*t*W0)),t,-(T0/2),(T0/2)));
CK=(1/T0)*int(t*exp(-j*k*W0*t),t,-(T0/2),(T0/2));
%SIMPLIFY THE COEF's
Ak=simplify (AK)
Bk= round(BK)
Ck=simplify (CK)
%INITIALIZE VARIABLES
dt=T0/100;
%CALCULATE A0
A0=(1/T0)*int(t),t,-(T0/2),(T0/2);
t=[-pi:dt:pi]; %the variable limits and resolution
Y=zeros(size(t));
%PLOT FUNCTION
for k=[1:2:21];
y=A0/2+Ak*cos(k*t*W0)+Bk*sin(k*t*W0);
%double(y);
%EZPLOT(y);
% Y=Y+y
end

%plot(t,Y,'LineWidth',2);
axis([-4 4 -4 4]);
grid;
xlabel('time, s');
ylabel('v1(t), (V)');

%%%Grafica de series de fourier


----------------------------------------------------------------------
N = Number of points plotted
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = number of coefficients
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, �r�)
hold on
plot(x,f,�LineWidth�,2)
hold on
error = abs(sum-F)
Plot(x, error);
--------------------------------------------------------------
disp('Serie de Fourier')
N= NUMERO DE ARM�NICOS DESEADOS;
x=-2:0.01:2;
sum=0;
for k=1:2:N
b(k)=4/(k*pi);
sum=sum+b(k)*sin(k*pi*x/4);
end
f=(x<0).*(-1)+(x>=0).*1;
plot(x,f,'g',x,sum,'b')
grid
title('Aproximaci�n por Serie de Fourier')

You might also like