Fourier series verification code written in Python

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. Figure_1.png 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. Figure_2.png n = 0,1 is not displayed due to the for statement, but please change each here.

References

https://qiita.com/weedslayer/items/25c38c928ad59ba61071

Recommended Posts

Fourier series verification code written in Python
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
Generate QR code in Python
Gacha written in Python -BOX gacha-
Character code learned in Python
Comparison of exponential moving average (EMA) code written in Python
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Squid Lisp written in Python: Hy
Signal processing in Python (1): Fourier transform
[Python] Generate QR code in memory
Automatically format Python code in Vim
Division of timedelta in Python 2.7 series
Compatibility diagnosis program written in python
Write selenium test code in python
Simple gacha logic written in Python
Code tests around time in Python
Stress Test with Locust written in Python
Markov chain transition probability written in Python
Get the EDINET code list in Python
Adding Series to columns in python pandas
Notes on using code formatter in Python
Introduction to Effectiveness Verification Chapter 1 in Python
Get time series data from k-db.com in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
How to use Python Image Library in python3 series
Epoch in Python
Personal notes to doc Python code in Sphinx
Discord in Python
I wrote "Introduction to Effect Verification" in Python
Sudoku in Python
DCI in Python
python character code
quicksort in python
Uncertainty of Japanese unide code in Tacotron 2 series
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
[Python] Algorithm-aware code
Sqlite in python
StepAIC in Python
Code often used in Python / Django apps [prefectures]
N-gram in python
Use Python in Anaconda environment with VS Code
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python