fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # フーリエ係数
  5. a = [0, 2/np.pi**2, 0, 2/(9*np.pi**2), 0]
  6. b = [2/np.pi, 4/np.pi**2, 2/(3*np.pi), 4/(9*np.pi**2), 2/(5*np.pi)]
  7.  
  8. # 関数を定義
  9. def f(x, n):
  10. result = np.pi/2 # 直流成分
  11. for i in range(len(n)):
  12. result += np.sqrt(a[i]**2 + b[i]**2) * np.sin(n[i] * x)
  13. return result
  14.  
  15. # x軸の値
  16. x_values = np.linspace(-np.pi, np.pi, 1000)
  17.  
  18. # 関数の値を計算
  19. y_values = f(x_values, [1, 2, 3, 4, 5])
  20.  
  21. # グラフを描画
  22. plt.figure(figsize=(8, 6))
  23. plt.plot(x_values, y_values, label='f(x)')
  24. plt.xlabel('x')
  25. plt.ylabel('f(x)')
  26. plt.title('Graph of f(x)')
  27. plt.grid(True)
  28. plt.legend()
  29. plt.show()# your code goes here
Success #stdin #stdout 0.66s 55356KB
stdin
Standard input is empty
stdout
Standard output is empty