# -*- coding: utf-8 -*-
"""
RMS 1047 - série trigonométrique, égalité de Parseval
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad

def g(x, N=100):
    s = np.pi**2/3
    for n in range(1, N):
        s += 4*(-1)**n*np.cos(n*x)/n**2
    return s

plt.figure()
X = np.linspace(-2*np.pi, 3*np.pi, 200)
Y = [ g(x) for x in X ]
plt.plot(X, Y)

def f(x):
    return x**2
    
def a(n):
    def integrande(x):
        return f(x)*np.cos(n*x)
    return 1/(np.pi)*quad(integrande, -np.pi, np.pi)[0]
    
N = 20
u_n = [ 4*(-1)**n/n**2 for n in range(1, N) ]
coeffs_Fourier = [ a(n) for n in range(1, N) ]

plt.figure()
plt.plot(u_n, coeffs_Fourier, '+')
plt.axis('equal')