fork download
  1.  
Success #stdin #stdout 0.07s 14200KB
stdin
import numpy as np
import matplotlib.pyplot as plt

# Parameters
T = 1
A = 1
omega0 = 2 * np.pi / T

# Time axis (1 ms step)
t = np.arange(0, 1 + 0.001, 0.001)

# Fourier series synthesis
f = np.zeros_like(t)
for n in range(1, 102, 2):  # odd numbers 1,3,5,...,101
    f += (2*A/np.pi) * (1/n) * np.cos(n * omega0 * t)

# Plot
plt.figure(figsize=(10, 4))
plt.plot(t, f, linewidth=1.2)
plt.title("Fourier Series Approximation of Square Wave (n = 1 to 101)")
plt.xlabel("t [s]")
plt.ylabel("f(t)")
plt.grid(True)

# Save PNG
plt.savefig("square_wave_fourier_101.png")
plt.close()
stdout
Standard output is empty