Fourier sine series
#Introduction#
Wait a few seconds until Python interaction ready! is shown in the top.
After that follow the instructions in the Grasple Exercise.
On this webpage the square wave function
is considered, where \(P\pi\) is the period of the wave and \(A\) is the (signed) amplitude of the wave.
This webpage constructs the truncated Fourier series
using numpy, and then uses numpy and pyplot to display the obtained exact values.
import micropip
await micropip.install("ipympl")
import matplotlib
if not hasattr(matplotlib.rcParams, '_get'):
matplotlib.rcParams._get = matplotlib.rcParams.get
%matplotlib widget
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
def Fourier_sine_series(A,P,m,fig,m_min=0):
# cast to int to be sure
m = int(m)
m_min=int(m_min)
def f(x_vec,A,P):
A = float(A)
x_vec_copy = np.copy(x_vec)
if len(x_vec)>1:
dx = np.mean(np.diff(x_vec_copy))
else:
dx = 1e-16
I = np.where(x_vec_copy<-P/2*np.pi)[0]
while len(I)>0:
x_vec_copy[I] = x_vec_copy[I]+P*np.pi
I = np.where(x_vec_copy<-P/2*np.pi)[0]
I = np.where(x_vec_copy>=P/2*np.pi)[0]
while len(I)>0:
x_vec_copy[I] = x_vec_copy[I]-P*np.pi
I = np.where(x_vec_copy>=P/2*np.pi)[0]
func_vec = np.zeros_like(x_vec_copy)
func_vec_plot1 = np.zeros_like(x_vec_copy)
func_vec_plot2 = np.ones_like(x_vec_copy)*np.nan
I = np.where(x_vec_copy>0)[0]
func_vec[I] = A
func_vec_plot1[I] = A
I = np.where(np.isclose(x_vec_copy,0,rtol=0, atol=dx/2))[0]
if len(I)>0:
func_vec[I] = 0.5*A
func_vec_plot1[I] = np.nan
func_vec_plot2[I] = 0.5*A
I = np.where(np.isclose(x_vec_copy,P/2*np.pi,rtol=0, atol=dx/2))[0]
if len(I)>0:
func_vec[I] = 0.5*A
func_vec_plot1[I] = np.nan
func_vec_plot2[I] = 0.5*A
I = np.where(np.isclose(x_vec_copy,-P/2*np.pi,rtol=0, atol=dx/2))[0]
if len(I)>0:
func_vec[I] = 0.5*A
func_vec_plot1[I] = np.nan
func_vec_plot2[I] = 0.5*A
return func_vec,func_vec_plot1,func_vec_plot2
def sm(x_vec,m,A,P):
A = float(A)
func_vec = A*0.5*np.ones_like(x_vec)
for n in range(1,m+1):
func_vec += 2*A/(np.pi)/(2*n-1)*np.sin((2*n-1)*2/P*x_vec)
return func_vec
def em(x_vec,m,A,P):
f_vec = f(x_vec,A,P)[:][0]
sm_vec = sm(x_vec,m,A,P)
func_vec = np.abs(f_vec-sm_vec)
return func_vec
plt.close(fig)
if fig==1:
x_vec = np.linspace(-3/2*P*np.pi,3/2*P*np.pi,1001)
f_vec,f_vec_plot1,f_vec_plot2 = f(x_vec,A,P)
sm_vec = sm(x_vec,m,A,P)
figi = plt.figure(1)
plt.plot(x_vec/np.pi,sm_vec,label='$s_m(x)$',lw=3)
plt.plot(x_vec/np.pi,f_vec_plot1,'orange',label='$f(x)$')
plt.plot(x_vec/np.pi,f_vec_plot2,'o',color='orange')
ax = plt.gca()
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))
ax.xaxis.set_major_locator(tck.MultipleLocator(base=P/2))
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend()
plt.show();
figi.canvas.toolbar_visible = True
if fig==2:
x_vec = np.linspace(-1/2*P*np.pi,1/2*P*np.pi,1001)
em_vec = em(x_vec,m,A,P)
figi = plt.figure(2)
plt.plot(x_vec/np.pi,em_vec,'o',label='$e_m(x)$',lw=3,markersize=2)
ax = plt.gca()
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))
ax.xaxis.set_major_locator(tck.MultipleLocator(base=P/4))
plt.xlabel('$x$')
plt.ylabel('$y$')
L = plt.legend()
plt.show();
figi.canvas.toolbar_visible = True
if fig==3:
x = np.array([-3])
if m<m_min:
m_min=np.max([0,m-10])
m_vec = np.arange(m_min,m+1)
em_vec = np.zeros_like(m_vec,dtype=float)
for i,mi in enumerate(m_vec):
em_vec[i] = em(x,mi,A,P)[0]
figi = plt.figure(3)
plt.plot(m_vec,em_vec,label='$e_m(-3)$',marker='o',linestyle='None')
plt.plot(m_vec,np.abs(A)*0.001*np.ones_like(em_vec),label='$0.001|A|$',linestyle='--')
plt.xlabel('$m$')
plt.ylabel('$error$')
L = plt.legend()
plt.show();
figi.canvas.toolbar_visible = True
pass
INPUT CELL 1#
In the cell below you should put the values of the parameters you see in Grasple.
Please note: Each attempt in Grasple contains different values for the parameters.
After you run the cell the results of the calculations are shown with the set parameter values.
If you point your mouse below a figure, controls will appear that you can use to pan and zoom.
''' Set the given values in the following lines '''
A = -3
P = 3
m = 4
''' The next line performs the calculations and shows the results. '''
Fourier_sine_series(A,P,m,1)
INPUT CELL 2#
In the cell below you should put the values of the parameters you see in Grasple.
Please note: Each attempt in Grasple contains different values for the parameters.
After you run the cell the results of the calculations are shown with the set parameter values.
If you point your mouse below a figure, controls will appear that you can use to pan and zoom.
''' Set the given values in the following lines '''
m = 8
''' The next line performs the calculations and shows the results. '''
Fourier_sine_series(A,P,m,2)
INPUT CELL 3#
In the cell below you should put the values of the parameters you see in Grasple.
Please note: Each attempt in Grasple contains different values for the parameters.
After you run the cell the results of the calculations are shown with the set parameter values.
If you point your mouse below a figure, controls will appear that you can use to pan and zoom.
''' Set the given values in the following lines '''
m = 15
m_min = 0
''' The next line performs the calculations and shows the results. '''
Fourier_sine_series(A,P,m,3,m_min)