#範例11-8:求週期Square Wave方波函數f(x),在(-2 < x < 2)的傅立葉級數Fourier Series #本題的方波函數square wave #f(x) = 0, 當 -2 < x < -1 #f(x) = 1, 當 -1< x < 1 #f(x) = 0, 當 1 < x < 2 from sympy import * x = symbols('x') f = Function('f')(x) #注意:使用Piecewise,複合局部區域,1< x < 2,的表示方法為:(x<-1) & (x>-2) f = Piecewise((0, (x>1)&(x<2)), (1, (x>-1)&(x<1)), (0, (x<-1)&(x>-2))) #plot(f) #轉成傅立葉級數 s = fourier_series(f, (x, -2, 2)) #截短成n=8, n=30 fs8 = s.truncate(n=8) fs30 = s.truncate(n=30) #畫圖 p1 = plot(fs8,fs30,show=False) p1[0].line_color = 'red' p1[1].line_color = 'green' p1[0].label = '$fourier(n=8)$' p1[1].label = '$fourier(n=30)$' p1.legend=True p1.show() #結果1:不管n=8,或是n=30,都會有gibbs phenomenon吉布斯跳動