Exploring Fourier Series using Python and SymPy in Google Colab
Are you curious about Fourier series and how they can be used to represent periodic functions? Look no further than this Python code using the SymPy library!
This code defines a function that generates the Fourier series of the function \(f(x)=x\) between \(-2\) and \(2\), with a period of \(2\). By adjusting the number of terms in the series, you can see how the approximation of the function improves. The resulting plot is displayed using the interactive capabilities of ipywidgets, allowing you to easily explore different values of \(k\).
Whether you're a seasoned mathematician or just starting out, this code is a great way to learn about Fourier series and explore their properties in a hands-on way. Give it a try and see what insights you can gain!
from sympy import *
from sympy.abc import x
from ipywidgets import interact, interactive,fixed
import ipywidgets as widgets
%matplotlib inline
def g(k):
'''
The fourier series of the function `f(x)=x` defined between -2 and 2, with period 2
where `f(x+2)=f(x)`*
'''
f_x = x
s = fourier_series(f_x, (x, -2, 2))
fourierSeries = s.truncate(k) # The first k terms are used
plot(fourierSeries,size=(12,6))
w = interactive(g,k=widgets.IntSlider(min=1,max=30,step=1,value=5,description='N of terms'))
display(w)