# Chapter 10 : Fourier Series¶

## Example 10.1, page no. 327¶

In [20]:
import sympy,math,numpy

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 1/math.pi*sympy.integrate(sympy.exp(-1*x),(x,0,2*math.pi))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range (1,n+1):
ai = 1/math.pi*sympy.integrate(sympy.exp(-x)*sympy.cos(i*x),(x,0,2*math.pi))
bi = 1/math.pi*sympy.integrate(sympy.exp(-x)*sympy.sin(i*x),(x,0,2*math.pi))
s = s+float(ai)*sympy.cos(i*x)+float(bi)*sympy.sin(i*x)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 3
0.158857730350203*sin(x) + 0.127086184280162*sin(2*x) + 0.0953146382101218*sin(3*x) + 0.158857730350203*cos(x) + 0.0635430921400812*cos(2*x) + 0.0317715460700406*cos(3*x) + 0.158857730350203


## Example 10.2, page no. 328¶

In [11]:
import numpy,math,sympy

print "To find the fourier transform of given function"
x = sympy.Symbol('x')
s = sympy.Symbol('s')
F = sympy.integrate(sympy.exp(1j*s*x),(x,-1,1))
print F
F1 = sympy.integrate(sympy.sin(x)/x,(x,0,numpy.inf))
print F1

To find the fourier transform of given function
Piecewise((2, s == 0), (1.0*I*exp(-1.0*I*s)/s - 1.0*I*exp(1.0*I*s)/s, True))
1.57079632679490


## Example 10.3, page no. 328¶

In [13]:
import numpy,sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 1/math.pi*(sympy.integrate(-1*math.pi*x**0,(x,-math.pi,0))+sympy.integrate(x,(x,0,math.pi)))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range (1,n+1):
ai = 1/math.pi*(sympy.integrate(-1*math.pi*sympy.cos(i*x),(x,-1*math.pi,0))+sympy.integrate(x*sympy.cos(i*x),(x,0,math.pi)))
bi = 1/math.pi*(sympy.integrate(-1*math.pi*x**0*sympy.sin(i*x),(x,-1*math.pi,0))+sympy.integrate(x*sympy.sin(i*x),(x,0,math.pi)))
s = s+float(ai)*sympy.cos(i*x)+float(bi)*sympy.sin(i*x)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 3
3.0*sin(x) - 0.5*sin(2*x) + 1.0*sin(3*x) - 0.636619772367581*cos(x) - 0.0707355302630646*cos(3*x) - 0.785398163397448


## Example 10.4, page no. 329¶

In [15]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
l = sympy.Symbol('l')
ao = 1/l*sympy.integrate(sympy.exp(-1*x),(x,-l,l))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion :"))
for i in range (1,n+1):
ai = 1/l*sympy.integrate(sympy.exp(-x)*sympy.cos(i*math.pi*x/l),(x,-l,l))
bi = 1/l*sympy.integrate(sympy.exp(-x)*sympy.sin(i*math.pi*x/l),(x,-l,l))
s = s+float(ai)*sympy.cos(i*math.pi*x/l)+float(bi)*sympy.sin(i*math.pi*x/l)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion :3
(exp(l) - exp(-l))/(2*l)


## Example 10.5, page no. 330¶

In [17]:
import math,sympy

print "finding the fourier series of given function"
x = sympy.Symbol('x')
l = sympy.Symbol('l')
s = 0
n = int(raw_input("enter the no of terms up to each of terms in the expansion : "))
for i in range(1,n+1):
bi = 2/math.pi*sympy.integrate(x*sympy.sin(i*x),(x,0,math.pi))
s = s+float(bi)*sympy.sin(i*x)
print s

finding the fourier series of given function
enter the no of terms up to each of terms in the expansion : 3
2.0*sin(x) - 1.0*sin(2*x) + 0.666666666666667*sin(3*x)


## Example 10.6, page no. 332¶

In [19]:
import math,sympy

print "finding the fourier series of given function"
x = sympy.Symbol('x')
l = sympy.Symbol('l')
ao = 2/l*sympy.integrate(x**2,(x,0,l))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion :"))
for i in range(1,n+1):
ai = 2/l*sympy.integrate(x**2*sympy.cos(i*math.pi*x/l),(x,0,l))
s = s+float(ai)*sympy.cos(i*math.pi*x/l)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion :3
l**2/3


## Example 10.7, page no. 333¶

In [1]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 2/math.pi*(sympy.integrate(sympy.cos(x),(x,0,math.pi/2))+sympy.integrate(-sympy.cos(x),(x,math.pi/2,math.pi)))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion :"))
for i in range(1,n+1):
ai = 2/math.pi*(sympy.integrate(sympy.cos(x)*sympy.cos(i*x),(x,0,math.pi/2))+sympy.integrate(-sympy.cos(x)*sympy.cos(i*x),(x,math.pi/2,math.pi)))
s = s+float(ai)*sympy.cos(i*x)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion :2
7.06789929214115e-17*cos(x) + 0.424413181578387*cos(2*x) + 0.636619772367581


## Example 10.8, page no. 334¶

In [3]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 2/math.pi*(sympy.integrate((1-2*x/math.pi),(x,0,math.pi)))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range(1,n+1):
ai = 2/math.pi*(sympy.integrate((1-2*x/math.pi)*sympy.cos(i*x),(x,0,math.pi)))
s = s+float(ai)*sympy.cos(i*x)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 3
0.810569469138702*cos(x) + 7.79634366503875e-17*cos(2*x) + 0.0900632743487446*cos(3*x)


## Example 10.9, page no. 336¶

In [9]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
l = sympy.Symbol('l')
s = 0
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range(1,n+1):
bi = sympy.integrate(x*sympy.sin(i*math.pi*x/2),(x,0,2))
s = s+float(bi)*sympy.sin(i*math.pi*x/2)
print float(s)

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 3
0.0


## Example 10.10, page no. 337¶

In [10]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 2/2*(sympy.integrate(x,(x,0,2)))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range(1,n+1):
ai = 2/2*(sympy.integrate(x*sympy.cos(i*math.pi*x/2),(x,0,2)))
s = s +float(ai)*sympy.cos(i*math.pi*x/2)
print float(s)

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 5
1.0


## Example 10.11, page no. 338¶

In [13]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 0
s = ao
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range(1,n+1):
bi = 2/1*(sympy.integrate((1/4-x)*sympy.sin(i*math.pi*x),(x,0,1/2))+sympy.integrate((x-3/4)*sympy.sin(i*math.pi*x),(x,1/2,1)))
s = s+float(bi)*sympy.sin(i*math.pi*x)
print float(s)

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 2
0.0


## Example 10.12, page no. 339¶

In [15]:
import sympy,math

print "finding the fourier series of given function"
x = sympy.Symbol('x')
ao = 1/math.pi*sympy.integrate(x**2,(x,-math.pi,math.pi))
s = ao/2
n = int(raw_input("enter the no of terms up to each of sin or cos terms in the expansion : "))
for i in range(1,n+1):
ai = 1/math.pi*sympy.integrate((x**2)*sympy.cos(i*x),(x,-math.pi,math.pi))
bi = 1/math.pi*sympy.integrate((x**2)*sympy.sin(i*x),(x,-math.pi,math.pi))
s = s+float(ai)*sympy.cos(i*x)+float(bi)*sympy.sin(i*x)
print s

finding the fourier series of given function
enter the no of terms up to each of sin or cos terms in the expansion : 3
-4.0*cos(x) + 0.999999999999999*cos(2*x) - 0.444444444444444*cos(3*x) + 3.28986813369645


## Example 10.13, page no. 341¶

In [13]:
import sympy,math

print "The complex form of series is summation of f(n,x) where n varies from −%inf to %inf and f(n,x) is given by : "
n = sympy.Symbol('n')
x = sympy.Symbol('x')
a = sympy.exp(-x)
b = sympy.exp(-1j*math.pi*n*x)
cn = 1/2*sympy.Integral(sympy.exp(-x)*sympy.exp(-1j*math.pi*n*x),(x,-sympy.oo,sympy.oo))
fnx = float(cn)*sympy.exp(1j*n*math.pi*x)
print fnx

The complex form of series is summation of f(n,x) where n varies from −%inf to %inf and f(n,x) is given by :
0


## Example 10.14, page no. 342¶

In [1]:
import sympy, math, numpy

print "Practical harmoninc analysis"
x = sympy.Symbol('x')
xo = numpy.array([math.pi/6, math.pi/3, math.pi/2, 2*math.pi/3, 5*math.pi/6, math.pi, 7*math.pi/6, 4*math.pi/3, \
3*math.pi/2, 5*math.pi/3, 11*math.pi/6])
yo = numpy.array([1.10, 0.30, 0.16, 1.50, 1.30, 2.16, 1.25, 1.30, 1.52, 1.76, 2.00])
ao = 2*numpy.sum(yo)/len(xo)
s = ao/2
n = int(raw_input('No of sin or cos term in expansion : '))
for i in range(1,n+1):
an = 2*sum(yo*numpy.cos(i*xo))/len(yo)
bn = 2*sum(yo*numpy.sin(i*xo))/len(yo)
s = s+float(an)*sympy.cos(i*x)+float(bn)*sympy.sin(i*x)
print s

Practical harmoninc analysis
No of sin or cos term in expansion : 5
-0.522944001594253*sin(x) - 0.410968418886797*sin(2*x) + 0.0927272727272729*sin(3*x) + 0.111796006670355*sin(4*x) - 0.126146907496654*sin(5*x) - 0.373397459621556*cos(x) + 0.159090909090909*cos(2*x) - 0.258181818181819*cos(3*x) - 0.257272727272728*cos(4*x) - 0.546602540378445*cos(5*x) + 1.30454545454545


## Example 10.15, page no. 342¶

In [11]:
import math,sympy,numpy

print "Practical harmonic analysis"
x = sympy.Symbol('x')
T = sympy.Symbol('T')
xo = numpy.array([1/6,1/3,1/2,2/3,5/6,1])
yo = numpy.array([1.30,1.05,1.30,-0.88,-0.25,1.98])
ao = 2*sum(yo)/len(xo)
s = ao/2
n = int(raw_input("No of sin or cos term in expansion :"))
i = 1
an = 2*sum(yo*numpy.cos(i*xo*2*math.pi))/len(yo)
bn = 2*sum(yo*numpy.sin(i*xo*2*math.pi))/len(yo)
s = s+float(an)*sympy.cos(i*x*2*math.pi/T)+float(bn)*sympy.sin(i*x*2*math.pi/T)
print s
print "Direct current :"
i = math.sqrt(an**2+bn**2)
print i

Practical harmonic analysis
No of sin or cos term in expansion :5
-1.61653377487451e-16*sin(6.28318530717959*x/T) + 1.5*cos(6.28318530717959*x/T) + 0.75
1.5 -1.61653377487e-16
Direct current :
1.5


## Example 10.16, page no. 343¶

In [19]:
import math,sympy,numpy

print "Practical harmonic analysis"
x = sympy.Symbol('x')
T = sympy.Symbol('T')
xo = int(raw_input("Input xo matrix (in factor of T):"))
yo = numpy.array([1.30,1.05,1.30,-0.88,-0.25,1.98])
ao = 2*sum(yo)/xo
s = ao/2
n = int(raw_input("No of sin or cos term in expansion :"))
i = 1
an = 2*sum(yo*numpy.cos(i*xo*2*math.pi))/len(yo)
bn = 2*sum(yo*numpy.sin(i*xo*2*math.pi))/len(yo)
s = s+float(an)*sympy.cos(i*x*2*math.pi/T)+float(bn)*sympy.sin(i*x*2*math.pi/T)
print s
print "Direct current :"
i = math.sqrt(an**2+bn**2)
print i

Practical harmonic analysis
Input xo matrix (in factor of T):1
No of sin or cos term in expansion :5
-3.67394039744206e-16*sin(6.28318530717959*x/T) + 1.5*cos(6.28318530717959*x/T) + 4.5
Direct current :
1.5


## Example 10.17, page no. 344¶

In [20]:
import math,sympy,numpy

print "Practical harmonic analysis"
x = sympy.Symbol('x')
T = sympy.Symbol('T')
xo = int(raw_input("Input xo matrix (in factor of T):"))
yo = numpy.array([1.30,1.05,1.30,-0.88,-0.25,1.98])
ao = 2*sum(yo)/xo
s = ao/2
n = int(raw_input("No of sin or cos term in expansion :"))
i = 1
an = 2*sum(yo*numpy.cos(i*xo*2*math.pi))/len(yo)
bn = 2*sum(yo*numpy.sin(i*xo*2*math.pi))/len(yo)
s = s+float(an)*sympy.cos(i*x*2*math.pi/T)+float(bn)*sympy.sin(i*x*2*math.pi/T)
print s
print "Direct current :"
i = math.sqrt(an**2+bn**2)
print i

Practical harmonic analysis
Input xo matrix (in factor of T):1
No of sin or cos term in expansion :4
-3.67394039744206e-16*sin(6.28318530717959*x/T) + 1.5*cos(6.28318530717959*x/T) + 4.5
Direct current :
1.5