The troublesome thing for undergraduate students in science and engineering is the manual calculation of Fourier series. It's okay to do all the calculations, but often the results are too dirty and unconfident. So I wrote a code that can be checked with Python, so I will post it as a memorandum.
For example
f(x)=x \ \ \ \ (-\pi \le x \le \pi)
When the Fourier series is expanded
f(x)=-\Sigma_{n=1}^{\infty}\frac{(-1)^n}{n}\sin(nx)
Will be. Let's check this.
import numpy as np
import matplotlib.pyplot as plt
#Define the x-axis
x = np.arange(-np.pi,np.pi,0.01)
#Functions inside Sigma
def func(k):
return -1*(((-1)**k)/k)*np.sin(k*x)
#Sigma operation function.Specify the sum range with an argument.
def sigma(func,frm,to):
ret = np.zeros_like(x)
for i in range(frm,to):
ret += func(i)
return ret
#Fourier series expansion function
f = sigma(func,1,100)
#View results
fig, ax = plt.subplots(1,1)
ax.set_aspect('equal')
ax.plot(x,f)
plt.show()
#This is a graph to see the process of being added
#Up to 9 times with this setting
"""
fig, axes = plt.subplots(3,3,figsize=(7,7))
for i,ax in enumerate(axes.ravel()):
ax.set_aspect('equal')
ax.set_title('n={}'.format(i))
f = sigma(func,1,i)
ax.plot(x,f)
plt.show()
"""
The result looks like this. It is a combination of 100 terms, but it can be reproduced reasonably well.
If you then execute the commented out part of the code, the result will be as follows. n = 0,1 is not displayed due to the for statement, but please change each here.
https://qiita.com/weedslayer/items/25c38c928ad59ba61071
Recommended Posts