You are on page 1of 6

Name and register number: R Meghana, 20BEC1042

Date: 29/04/2021

Harmonic Series of a Discrete Periodic Function


Input
clc
clear all
syms t
n=input('Enter the number of data points n:');
x_0=input('Enter the starting value of x:');
count=input('Type 0 if the unit of x is degree; type a
non-zero number otherwise');
s=input('Enter the length of spacing between successive
values of x:');
n1=input('Enter the number of harmonic of the series
n1:');
for i=1:n
x(i)=x_0+(i-1)*s;
end
if(count==0)
x=x*pi/180;
s=s*pi/180;
end
y=input('Enter the y values as a row vector:')
l=0.5*(x(n)+s-x(1));%l=pi if it is a degree
a_0=(2/n)*sum(y)
for i=1:n
yc=y.*cos(i*pi*x/l);
ys=y.*sin(i*pi*x/l);
a(i)=(2/n)*sum(yc);
b(i)=(2/n)*sum(ys);
end
F_s=a_0/2;
for i=1:n1
subplot(n1,1,i)
plot(x,y,'r*')
hold on
F_s=F_s+a(i).*cos(i*pi*t/l)+b(i)*sin(i*pi*t/l);
subplot(n1,1,i)
ezplot(F_s,[x(1) x(n)])
end
disp('Fourier Series')
vpa(F_s,4)

Output

Enter the number of data points n:


6
Enter the starting value of x:
0
Type 0 if the unit of x is degree; type a non-zero number otherwise
4
Enter the length of spacing between successive values of x:
1
Enter the number of harmonic of the series n1:
3
Enter the y values as a row vector:
[4 8 15 7 6 2]

y =

4 8 15 7 6 2

a_0 =

14

Fourier Series

ans =
4.33*sin(1.047*t) - 1.5*cos(2.094*t) - 2.833*cos(1.047*t) -
0.866*sin(2.094*t) + 2.667*cos(3.142*t) + 5.719e-16*sin(3.142*t) +
7.0

Enter the number of data points n:


12
Enter the starting value of x:
0
Type 0 if the unit of x is degree; type a non-zero number otherwise
0
Enter the length of spacing between successive values of x:
30
Enter the number of harmonic of the series n1:
3
Enter the y values as a row vector:
[1.80 1.10 0.30 0.16 1.50 1.30 2.16 1.25 1.30 1.52 1.76 2.00]

y =

1.8000 1.1000 0.3000 0.1600 1.5000 1.3000


2.1600 1.2500 1.3000 1.5200 1.7600 2.0000

a_0 =

2.6917

Fourier Series

ans =

0.4458*cos(2.0*t) - 0.3767*sin(2.0*t) + 0.06333*cos(3.0*t) +


0.085*sin(3.0*t) - 0.04228*cos(t) - 0.4794*sin(t) + 1.346
Enter the number of data points n:
6
Enter the starting value of x:
0
Type 0 if the unit of x is degree; type a non-zero number otherwise
6
Enter the length of spacing between successive values of x:
1
Enter the number of harmonic of the series n1:
3
Enter the y values as a row vector:
[9 18 24 28 26 20]

y =

9 18 24 28 26 20

a_0 =

41.6667

Fourier Series

ans =

2.368e-15*sin(2.094*t) - 2.333*cos(2.094*t) - 1.155*sin(1.047*t) -


8.333*cos(1.047*t) - 2.333*cos(3.142*t) + 1.388e-14*sin(3.142*t) +
20.83

You might also like