傅立叶(Fourier)级数-实数形式-方波: 定义求解

文章介绍了如何使用Python的numpy库计算奇函数的积分,并通过Fourier级数近似方法,比较了n=1和n=1000时的结果,展示了离散逼近的精度。同时,使用matplotlib绘制了函数曲线和对应的Fourier近似曲线。
摘要由CSDN通过智能技术生成

题目

\boldsymbol{f(t)=\begin{cases} \frac{\pi}{4},&0<t<\pi \\ \frac{-\pi}{4},&-\pi<t<0 \end{cases}}

f(t)是奇函数

\boldsymbol{a_n=0, b_n=\frac{4}{T}\int_0^{\frac{T}{2}}f_T(t)sin(n\omega{t}) dt}

\boldsymbol{T=2\pi,\omega=1}

\boldsymbol{b_n=\frac{4}{2\pi}\int_0^{\pi}\frac{\pi}{4}sin(nt) dt}

\boldsymbol{ ...=\frac{1}{2}\int_0^{\pi}sin(nt) dt}

\boldsymbol{...=-\frac{1}{2n}cos(nt)\mid{_0^{\pi}}}

\boldsymbol{...=-\frac{1}{2n}[cos(n\pi)-1]}

\boldsymbol{...=\frac{1}{2n}[1-(-1)^n]}

\boldsymbol{f(t)=\frac{a_0}{2}+\displaystyle\sum_{n=1}^\infty [a_ncos(n\omega{t})+b_nsin(n\omega{t})], \omega=\frac{2\pi}{T}=1}

\boldsymbol{f(t)=\displaystyle\sum_{n=1}^\infty b_nsin(nt)=\displaystyle\sum_{n=1}^\infty \frac{1-(-1)^n}{2n}sin(nt)}

积分定义方式求bn

\boldsymbol{b_n =\frac{1}{2}\int_0^{\pi}sin(nt) dt}

import numpy as np

n = [1,2,3,4,5]

# 定义方式求bn
inte_s,inte_e = 0, np.pi
segNum = 1000000
t_points = np.linspace(inte_s,inte_e,segNum)
dis_bn = lambda i: np.sum(0.5*np.sin(i*t_points)*(inte_e-inte_s)/segNum)

print([(1-(-1)**i)/(2*i) for i in n])
print([dis_bn(i) for i in n])

结果如下

[1.0, 0.0, 0.3333333333333333, 0.0, 0.2]
[0.9999989999991774, 3.892893545362616e-16, 0.33333299999753263, 5.67757714215085e-17, 0.1999997999958876]

当n最大=1

import numpy as np
import matplotlib.pyplot as plt

_, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.xlim(-5,5)
plt.ylim(-1.2,1.2)

start,end = -np.pi,np.pi
points = 300
t = np.linspace(start,end,points)
f_t = np.empty_like(t)
f_t[t<0] = -np.pi/4
f_t[t>0] = np.pi/4
plt.scatter(t,f_t,marker='.',s=1,c='b')

series_num = 1
f = lambda x: sum([np.sin(n*x)*(1-(-1)**n)/(2*n) for n in range(1,series_num+1)])
s_t = [f(i) for i in t]
plt.scatter(t,s_t,marker='.',s=1,c='r')

# 定义方式求bn
inte_s,inte_e = 0, np.pi
segNum = 10000
t_points = np.linspace(inte_s,inte_e,segNum)
dis_bn = lambda i: np.sum(0.5*np.sin(i*t_points)*(inte_e-inte_s)/segNum)
dis_f = lambda x: sum([np.sin(n*x)*dis_bn(n) for n in range(1,series_num+1)])
dis_st = [dis_f(i) for i in t]
plt.scatter(t,dis_st,marker='.',s=1,c='g')

print(s_t[0:3])
print(dis_st[0:3])

plt.legend(['f',f'fourier series_{series_num} of f',f'dis fourier series_{series_num} of f'])
plt.savefig('f.jpg')
[-1.2246467991473532e-16, -0.0210124511284756, -0.04201562375005738]
[-1.2245243243941147e-16, -0.02101034971052498, -0.04201142184208315]

当n最大=1000

[-6.174402573257526e-14, -0.797723992790085, -0.7901074952664077]
[-6.157155973065276e-14, -0.7975267965718601, -0.7899939672157446]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值