import numpy as np
import matplotlib.pyplot as plt
def phi_series(x, N):
s = 0.0
for n in range(N):
s += (-1)**n * (2/5)**(n+1) / (n+1) * x**n
return s
def fourier_F(x, N):
s = np.pi**2 / 3
for n in range(1, N+1):
s += 4*(-1)**n / n**2 * np.cos(n*x)
s += 4*(-1)**n / n * np.sin(n*x)
return s
# ---- 画 Phi ----
x_phi = np.linspace(-2.4, 2.4, 500)
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
for N in [5, 10, 20]:
plt.plot(x_phi, phi_series(x_phi, N), label=f'N={N}')
plt.title('幂级数部分和逼近 $\\Phi(x)$') # 注意改成“幂级数”
plt.xlabel('x')
plt.legend()
plt.grid(True)
# ---- 画 F ----
x_F = np.linspace(-np.pi, np.pi, 500)
plt.subplot(1, 2, 2)
for N in [5, 10, 20]:
plt.plot(x_F, fourier_F(x_F, N), label=f'N={N}')
plt.plot(x_F, x_F**2 - 2*x_F, 'k--', linewidth=2, label='$F(x)$')
plt.title('Fourier 部分和逼近 $F$')
plt.xlabel('x')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
To embed this project on your website, copy the following code and paste it into your website's HTML: