math_functions.py
def fourier_expansion(fun,L,n):
    """
    fun:a periodic function
    L:interval length
    n:the number of harmonics
    """
    #tol = 1e-6
    x = np.linspace(-L,L,100)
    

    
    a0 = 1/L/2 * integrate.quad(fun,-L,L)[0]
    an = 1/L * np.array([integrate.quad(lambda x:fun(x)*cos(i*x*pi/L),-L,L)[0] for i in np.arange(n)+1])
    bn = 1/L * np.array([integrate.quad(lambda x:fun(x)*sin(i*x*pi/L),-L,L)[0] for i in np.arange(n)+1])
    approx = a0 + np.array([an[i]*cos(pi*j*x/L) + bn[i]*sin(pi*j*x/L) for i,j in enumerate(np.arange(n)+1)]).sum(axis = 0)

    """
    c0 = 1/L/2 * integrate.quad(fun,-a,a)[0]

    cn = np.array([1/(L)*(integrate.quad(lambda x:fun(x)*cos(i*x*pi/L),-L,L)[0] - 1j* \
                            integrate.quad(lambda x:fun(x)*sin(i*x*pi/L),-L,L)[0]) \
                   for i in np.arange(n)+1 ])
    approx = c0 + np.array([cn[i]*np.exp(1j*pi*j*x/L) for i,j in enumerate(np.arange(n)+1) ]).sum(axis = 0)
    """
    return approx,np.array([an,bn])

    fun:a periodic function
    L:interval length
    n:the number of harmonics
    
a periodic function
L:interval length
n:the number of harmonics
a periodic function L : interval length n : the number of harmonics
def fourier_expansion(fun,L,n):
    x = np.linspace(-L,L,100)
    a0 = 1/L/2 * integrate.quad(fun,-L,L)[0]
    an = 1/L * np.array([integrate.quad(lambda x:fun(x)*cos(i*x*pi/L),-L,L)[0] for i in np.arange(n)+1])
    bn = 1/L * np.array([integrate.quad(lambda x:fun(x)*sin(i*x*pi/L),-L,L)[0] for i in np.arange(n)+1])
    approx = a0 + np.array([an[i]*cos(pi*j*x/L) + bn[i]*sin(pi*j*x/L) for i,j in enumerate(np.arange(n)+1)]).sum(axis = 0)
    return approx,np.array([an,bn])
def fourier_expansion ( fun , L , n ) : x = np . linspace ( - L , L , 100 ) a0 = 1 / L / 2 * integrate . quad ( fun , - L , L ) [ 0 ] an = 1 / L * np . array ( [ integrate . quad ( lambda x : fun ( x ) * cos ( i * x * pi / L ) , - L , L ) [ 0 ] for i in np . arange ( n ) + 1 ] ) bn = 1 / L * np . array ( [ integrate . quad ( lambda x : fun ( x ) * sin ( i * x * pi / L ) , - L , L ) [ 0 ] for i in np . arange ( n ) + 1 ] ) approx = a0 + np . array ( [ an [ i ] * cos ( pi * j * x / L ) + bn [ i ] * sin ( pi * j * x / L ) for i , j in enumerate ( np . arange ( n ) + 1 ) ] ) . sum ( axis = 0 ) """
    c0 = 1/L/2 * integrate.quad(fun,-a,a)[0]

    cn = np.array([1/(L)*(integrate.quad(lambda x:fun(x)*cos(i*x*pi/L),-L,L)[0] - 1j* \
                            integrate.quad(lambda x:fun(x)*sin(i*x*pi/L),-L,L)[0]) \
                   for i in np.arange(n)+1 ])
    approx = c0 + np.array([cn[i]*np.exp(1j*pi*j*x/L) for i,j in enumerate(np.arange(n)+1) ]).sum(axis = 0)
    """ return approx , np . array ( [ an , bn ] )
fun:a periodic function
L:interval length
n:the number of harmonics
fun : a periodic function L : interval length n : the number of harmonics
"""
    fun:a periodic function
    L:interval length
    n:the number of harmonics
    """ #tol = 1e-6
 """
    c0 = 1/L/2 * integrate.quad(fun,-a,a)[0]

    cn = np.array([1/(L)*(integrate.quad(lambda x:fun(x)*cos(i*x*pi/L),-L,L)[0] - 1j* \
                            integrate.quad(lambda x:fun(x)*sin(i*x*pi/L),-L,L)[0]) \
                   for i in np.arange(n)+1 ])
    approx = c0 + np.array([cn[i]*np.exp(1j*pi*j*x/L) for i,j in enumerate(np.arange(n)+1) ]).sum(axis = 0)
    """
fun
L
n