# JK 2024.08.16 v.1 Program oblicza współczynniki szeregu Fouriera danej funkcji, # rysuje wykres funkcji i funkcji aproksymujacej, rysuje wykres widma
import numpy as np import matplotlib.pyplot as plt from scipy.integrate import quad
# Parametry L = np.pi # Okres (od -L do L) N = 10 # Liczba wyrazów w szeregu Fouriera
# Definicja funkcji f(x) = x def f(x): return x*x-0.5*x**3
# Współczynniki a_n i b_n a0 = (1 / L) * quad(f, -L, L)
def a_n(n): integrand = lambda x: f(x) * np.cos(n * np.pi * x / L) result, _ = quad(integrand, -L, L) return result / L
def b_n(n): integrand = lambda x: f(x) * np.sin(n * np.pi * x / L) result, _ = quad(integrand, -L, L) return result / L
# Rekonstrukcja funkcji z szeregu Fouriera def fourier_series(x, N): a0_term = a0 / 2 sum_terms = sum(a_n(n) * np.cos(n * np.pi * x / L) + b_n(n) * np.sin(n * np.pi * x / L) for n in range(1, N + 1)) return a0_term + sum_terms
# Zakres x dla wykresu x_values = np.linspace(-L, L, 500) f_values = f(x_values) fs_values = [fourier_series(x, N) for x in x_values]
# Obliczanie współczynników dla widma n_values = np.arange(1, N + 1) a_n_values = np.array([a_n(n) for n in n_values]) b_n_values = np.array([b_n(n) for n in n_values])
# Wykres funkcji i szeregu Fouriera plt.figure(figsize=(14, 6))