5. Fourier Series is one of the most useful mathematical fact in real life. You can look up wikipedia on where it gets used. The claim is quite simple. Similar to Taylor series, Fourier claims/prove that all preriodic functions can be written as a sum of sine and cosine of varying frequency. For function of period 2L, the sum can be written as ao f(x) = 2 +α₁ cos(7) + α² cos(27) + aз cos(3) + ... Ꮖ +b sin(¸²) + b² sin(2ò) + b² sin(¾Ãª½) + …. +... Before it gets too abstract, the cell below show how similar the sawtooth function is to the sum of a few sine and cosine. The function below is sawtooth function with period of 2(it repeats itself every length 2). Since the period is 2L, L = 1. That means we expect sawtooth(x)≈ +α₁ cos(x) +α2 cos(2πx) + αз cos(3лx) +….. ао 2 +b₁ sin(x) + b² sin(2πx) + b² sin(3Ã×) +…… We will learn the magical power that helps us figure out a's and b's later. Let us compare it with 2 magic(x) = 1- 2 2π sin(x) sin(2x) sin(3x) — 2 Зп -... [4] which means that α = 2, b¿ = − 2 and a₁ = a₂ = ... = 0. π 2 3 4 def sawtooth(x): return x%2 #only works in python. other languages will scream. x = np.linspace (0,5,100) y = [sawtooth(xx) for xx in x] plt.plot(x,y,label='sawtooth') 7 plt.grid() 8 9 def magic(x): 10 S = 0 11 S += 1 12 s += -2/pi*sin(pi*x) - 2/2./pi*sin(2*pi*x) - 2/3./pi*sin(3*pi*x) 13 return s 14 magically = [magic(xx) for xx in x] 15 plt.plot(x, magical_y, 'r-',label='magic') 16 plt.legend(loc='upper left') 15 10 0.5 0.0 sawtooth magic Let us talk about how the magic of figuring out a and b. We can show that x=2L 1 ai = L [ f(x) cos(in²) da x=0 x=2L 1 b₁ = L [ƒ(x) sin(iπ²) da dx x=0 You can actually prove this with basic calculus. The trick is pretty neat. See http://mathworld.wolfram.com/FourierSeries.html or various youtube videos on how to prove this. For example, in the sawtooth example above (L = 1), b₂ was found by using x=2L b₂ = sawtooth(x) sin (27) dr dx x=0 5.1) Consider the function below. Find a0, a1, a2, a3 and b₁, b₂, b3 and plot the fourier approximation using these terms along with the original function. Your plot should look similar to the one shown for sawtooth. Do not forget to change L to appropriate value. I recommend you writing a nice function to figure out a and b. 1 def step(x): 2 return 0.0 if x%3>(3./2) else 1.0 3 x = np.linspace(0,5,1000) 4 y = [step(xx) for xx in x] 5 plt.plot(x,y,label='step') 6 plt.grid() plt.ylim(-0.1,1.1) 7 5] (-0.1, 1.1) 10 0.8 ྴ་སྦ་ལྦ་ ྴ་ Python Pytho 6] . - 5.2) Do 5.1 but now with a0, a1, ..., a10 and b1, b2, ... b10 5.3) Consider the function below. Find α, a₁, a₂, a¸ and b₁, b², b¸ and plot the fourier approximation using these terms along with the original function. Your plot should look similar to the one shown for sawtooth. Do not forget to change L to appropriate value. 1 def circ(x): r = 1 3 xx = (x%(2*r)) 4 return sqrt(r**2-(xx-r)**2) x = np.linspace (0,5,1000) y = [circ(xx) for xx in x] 7 plt.plot(x,y,label='step') plt.grid() 9 plt.ylim(-0.1,1.1) (-0.1, 1.1) 1.0 0.8 0.6 0.4 0.2 0.0 1 5.4) Do 5.3 but now with a0, a1, ..., a10 and b₁, b₂, . . . b10 1 B --- Python Python Python Python
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
from math import sin, cos, exp, pi, sqrt
import math
def sawtooth(x):
return x%2 #only works in python. other languages will scream.
x = np.linspace(0,5,100)
y = [sawtooth(xx) for xx in x]
plt.plot(x,y,label='sawtooth')
plt.grid()
def magic(x):
s = 0
s += 1
s += -2/pi*sin(pi*x) - 2/2./pi*sin(2*pi*x) - 2/3./pi*sin(3*pi*x)
return s
magical_y = [magic(xx) for xx in x]
plt.plot(x, magical_y,'r-',label='magic')
plt.legend(loc='upper left')
5.1
def step(x):
return 0.0 if x%3>(3./2) else 1.0
x = np.linspace(0,5,1000)
y = [step(xx) for xx in x]
plt.plot(x,y,label='step')
plt.grid()
plt.ylim(-0.1,1.1)
5.3
def circ(x):
r = 1
xx = (x%(2*r))
return sqrt(r**2-(xx-r)**2)
x = np.linspace(0,5,1000)
y = [circ(xx) for xx in x]
plt.plot(x,y,label='step')
plt.grid()
plt.ylim(-0.1,1.1)
Step by step
Solved in 2 steps