You are on page 1of 5

29/08/2020 1840463_29_08_2020_Fourier_Series_Lab03

LAB 03

Date: 29-08-2020

Topic : Fourier Series and Fourier Transform

Aim : To understand and execute Fourier series and


Fourier Transform problems.

Some Standard Integrals :


π π
1) ∫−π sin(nx)dx =∫−π cos(nx)dx = 0

−π
2) ∫π sin(mx)cos(nx)dx =0

π π m = n
3) ∫−π sin(mx)sin(nx)dx = {
0 m ≠ n

In [1]:

#1 (for sin(nx))
import numpy as np
import sympy as sp
x=sp.Symbol('x')
n=int(input("Enter the value of n:"))
f=sp.integrate(sp.sin(n*x),(x,-1*np.pi,np.pi))
print(f)

Enter the value of n:2


0

In [2]:

#1 (for cos(nx))
import numpy as np
import sympy as sp
x=sp.Symbol('x')
n=int(input("Enter the value of n:"))
f=round(sp.integrate(sp.cos(n*x),(x,-1*np.pi,np.pi)))
print(f)

Enter the value of n:5


0

localhost:8888/nbconvert/html/Downloads/1840463_29_08_2020_Fourier_Series_Lab03.ipynb?download=false 1/5
29/08/2020 1840463_29_08_2020_Fourier_Series_Lab03

In [3]:

#2
import numpy as np
import sympy as sp
x=sp.Symbol('x')
n=int(input("Enter the value of n:"))
f=sp.integrate(sp.sin(n*x)*sp.cos(n*x),(x,-1*np.pi,np.pi))
print(f)

Enter the value of n:5


0

In [2]:

#3
import numpy as np
import sympy as sp
x=sp.Symbol('x')
m=int(input("Enter the value of m:"))
n=int(input("Enter the value of n:"))
f=sp.integrate(sp.sin(m*x)*sp.sin(n*x),(x,-1*np.pi,np.pi))
print(f)

Enter the value of m:5


Enter the value of n:4
0

Fourier Series

f (x) = a0 /2 +∑n=1 an cos(nx) + bn sin(nx)

f (x) = ∑n=1 a0 /2 + an cos(nx) + bn sin(nx)

where,
π
ao = 1/π ∫ f (x)dx
−π

π
an = 1/π ∫ f (x)cos(nx)dx
−π

π
bn = 1/π ∫ f (x)sin(nx)dx
−π

Q.1) Find the fourier series expansion of f (x) = x


2
in (−π, π)

localhost:8888/nbconvert/html/Downloads/1840463_29_08_2020_Fourier_Series_Lab03.ipynb?download=false 2/5
29/08/2020 1840463_29_08_2020_Fourier_Series_Lab03

In [4]:

x=sp.Symbol('x')
ao=1/np.pi*sp.integrate(x**2,(x,-np.pi,np.pi))
s=ao/2
n=int(input('Enter the no. of terms up to each of sin or cos terms in the expre
ssion:'))
for i in range(1,n+1):
ai=1/np.pi*sp.integrate(x**2*sp.cos(i*x),(x,-np.pi,np.pi))
bi=1/np.pi*sp.integrate(x**2*sp.sin(i*x),(x,-np.pi,np.pi))
s=s+float(ai)*sp.cos(i*x)+float(bi)*sp.sin(i*x)
print(s)

Enter the no. of terms up to each of sin or cos terms in the expre
ssion:4
-4.0*cos(x) + 0.999999999999999*cos(2*x) - 0.444444444444444*cos(3
*x) + 0.249999999999999*cos(4*x) + 3.28986813369645

Q.2) Find the fourier series expansion of f (x) = e−x in (0, 2π)

In [5]:

x=sp.Symbol('x')
ao=1/np.pi*sp.integrate(sp.exp(-1*x),(x,0,2*np.pi))
s=ao/2
n=int(input('Enter the no. of terms up to each of sin or cos terms in the expre
ssion:'))
for i in range(1,n+1):
ai=1/np.pi*sp.integrate(sp.exp(-1*x)*sp.cos(i*x),(x,0,2*np.pi))
bi=1/np.pi*sp.integrate(sp.exp(-1*x)*sp.sin(i*x),(x,0,2*np.pi))
s=s+float(ai)*sp.cos(i*x)+float(bi)*sp.sin(i*x)
print(s)

Enter the no. of terms up to each of sin or cos terms in the expre
ssion:7
0.158857730350203*sin(x) + 0.127086184280162*sin(2*x) + 0.09531463
82101218*sin(3*x) + 0.0747565789883308*sin(4*x) + 0.06109912705777
04*sin(5*x) + 0.0515214260595253*sin(6*x) + 0.0444801644980568*sin
(7*x) + 0.158857730350203*cos(x) + 0.0635430921400812*cos(2*x) +
0.0317715460700406*cos(3*x) + 0.0186891447470827*cos(4*x) + 0.0122
198254115541*cos(5*x) + 0.00858690434325422*cos(6*x) + 0.006354309
21400812*cos(7*x) + 0.158857730350203

Fourier series of function having points of


discontinuity

π −π ≤ x ≤ 0
Q.3) Find the fourier series expansion of f (x) = {
0 0 < x ≤ π

localhost:8888/nbconvert/html/Downloads/1840463_29_08_2020_Fourier_Series_Lab03.ipynb?download=false 3/5
29/08/2020 1840463_29_08_2020_Fourier_Series_Lab03

In [6]:

x=sp.Symbol('x')
ao=1/np.pi*(sp.integrate(np.pi,(x,-np.pi,0))+sp.integrate(0,(x,0,np.pi)))
s=ao/2
n=int(input('Enter the no. of terms up to each of sin or cos terms in the expre
ssion:'))
for i in range(1,n+1):
ai=1/np.pi*(sp.integrate(np.pi*sp.cos(1*x),(x,-np.pi,0))+sp.integrate(0*sp.
cos(1*x),(x,-np.pi,0))
+sp.integrate(0*sp.cos(1*x),(x,0,np.pi)))
bi=1/np.pi*(sp.integrate(np.pi*sp.sin(1*x),(x,-np.pi,0))+sp.integrate(0*sp.
sin(1*x),(x,-np.pi,0))
+sp.integrate(0*sp.sin(1*x),(x,0,np.pi)))
s=s+float(ai)*sp.cos(i*x)+float(bi)*sp.sin(i*x)
print(s)

Enter the no. of terms up to each of sin or cos terms in the expre
ssion:4
-2.0*sin(x) - 2.0*sin(2*x) - 2.0*sin(3*x) - 2.0*sin(4*x) + 1.22464
679914735e-16*cos(x) + 1.22464679914735e-16*cos(2*x) + 1.224646799
14735e-16*cos(3*x) + 1.22464679914735e-16*cos(4*x) + 1.57079632679
49

−π −π ≤ x ≤ 0
Q.4) Find the fourier series expansion of f (x) = {
x 0 < x ≤ π

In [7]:

x=sp.Symbol('x')
ao=1/np.pi*(sp.integrate(-np.pi,(x,-np.pi,0))+sp.integrate(x,(x,0,np.pi)))
s=ao/2
n=int(input('Enter the no. of terms up to each of sin or cos terms in the expan
sion:'))
for i in range(1,n+1):
ai=1/np.pi*(sp.integrate(-np.pi*sp.cos(i*x),(x,-np.pi,0))+sp.integrate(x*sp
.cos(i*x),(x,0,np.pi)))
bi=1/np.pi*(sp.integrate(-np.pi*sp.sin(i*x),(x,-np.pi,0))+sp.integrate(x*sp
.sin(i*x),(x,0,np.pi)))
s=s+float(ai)*sp.cos(i*x)+float(bi)*sp.sin(i*x)
print(s)

Enter the no. of terms up to each of sin or cos terms in the expan
sion:3
3.0*sin(x) - 0.5*sin(2*x) + 1.0*sin(3*x) - 0.636619772367581*cos
(x) - 1.22355769773478e-18*cos(2*x) - 0.0707355302630646*cos(3*x)
- 0.785398163397448

⎧ −1 −π ≤ x ≤ −π/2

Q.5) Find the fourier series expansion of f (x) = ⎨ 0 −π/2 < x ≤ −π/2

1 −π/2 < x ≤ π

localhost:8888/nbconvert/html/Downloads/1840463_29_08_2020_Fourier_Series_Lab03.ipynb?download=false 4/5
29/08/2020 1840463_29_08_2020_Fourier_Series_Lab03

In [8]:

x=sp.Symbol('x')
ao=1/np.pi*(sp.integrate(-1,(x,-np.pi,-np.pi/2))+sp.integrate(0,(x,-np.pi/2,np.
pi/2))
+sp.integrate(1,(x,np.pi/2,np.pi)))
s=ao/2
n=int(input('Enter the no. of terms up to each of sin or cos terms in the expan
sion:'))
for i in range(1,n+1):
ai=1/np.pi*(sp.integrate(-1*sp.cos(i*x),(x,-np.pi,-np.pi/2))
+sp.integrate(0*sp.cos(i*x),(x,-np.pi/2,np.pi/2))
+sp.integrate(1*sp.cos(i*x),(x,np.pi/2,np.pi)))
bi=1/np.pi*(sp.integrate(-1*sp.sin(i*x),(x,-np.pi,-np.pi/2))
+sp.integrate(0*sp.sin(i*x),(x,-np.pi/2,np.pi/2))
+sp.integrate(1*sp.sin(i*x),(x,np.pi/2,np.pi)))
s=s+float(ai)*sp.cos(i*x)+float(bi)*sp.sin(i*x)
print(s)

Enter the no. of terms up to each of sin or cos terms in the expan
sion:2
0.636619772367581*sin(x) - 0.636619772367581*sin(2*x)

CONCLUSION:

In this session we learnt about Fourier series. We verified some of the standard integrals and
found out some major fourier series expansion.

localhost:8888/nbconvert/html/Downloads/1840463_29_08_2020_Fourier_Series_Lab03.ipynb?download=false 5/5

You might also like