%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import math
# Demonstration of how to find Fourier coefficients
import numpy, pylab, math
x = numpy.linspace(0,2*math.pi, 100)
def wave1(x):
b1 = 5
b3 = 2
return b1 * math.sin(x) + b3 * math.sin(3*x)
wave = map(wave1, x)
plt.plot(x,wave)
[<matplotlib.lines.Line2D at 0x10dbe7110>]
def integrate(f, start, end, step=0.1):
return sum(map(lambda x: f(x), numpy.arange(start, end, step))) * step
integrate(math.sin, 0, math.pi) # Testing integration function (should come out to 2)
1.999547959712598
plt.plot(x, map(lambda x: math.sin(x)*wave1(x), x))
plt.show()
plt.plot(x, map(lambda x: math.sin(3*x)*wave1(x), x))
plt.show()
T = 2 * math.pi # Fundamental period
ang_velocity = 1 # Radian/sec
n = 1
b1 = (2/T) * integrate(lambda x: math.sin(n * ang_velocity * x) * wave1(x), 0, T)
print "Fourier coefficient b1:", b1
Fourier coefficient b1: 5.00005433925
n = 3
b3 = (2/T) * integrate(lambda x: math.sin(n * ang_velocity * x) * wave1(x), 0, T)
print "Fourier coefficient b3:", b3
Fourier coefficient b3: 2.00016363946