import numpy as np
import matplotlib.pyplot as plt

# Definir la función f(x) = x + π
def f(x):
    return x + np.pi

# Calcular los coeficientes de la serie de Fourier
def fourier_coefficients(n):
    if n == 0:
        return np.pi
    elif n % 2 == 1:
        return (-2 / (n * np.pi)) * (1 - np.cos(n * np.pi))
    else:
        return 0

# Definir la serie de Fourier truncada
def fourier_series(x, terms=5):
    series = np.zeros_like(x)
    for n in range(1, terms+1):
        series += fourier_coefficients(n) * np.sin(n * x)
    return series

# Calcular la serie de Fourier truncada
x_values = np.linspace(-np.pi, np.pi, 400)
y_values = f(x_values)
series_values = fourier_series(x_values, terms=5)

# Graficar la función y su serie de Fourier truncada
plt.figure(figsize=(8, 6))
plt.plot(x_values, y_values, label='f(x)', color='blue')
plt.plot(x_values, series_values, label='Fourier Series (n=5)', color='red', linestyle='--')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Serie de Fourier truncada para f(x) = x + π')
plt.grid(True)
plt.legend()
plt.show()

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: