本文代碼: Fourier級(jí)數(shù)和Taylor級(jí)數(shù)對(duì)原函數(shù)的逼近動(dòng)畫
Taylor級(jí)數(shù)
級(jí)數(shù)是對(duì)已知函數(shù)的一種逼近,比較容易理解的是Taylor級(jí)數(shù),通過(guò)多項(xiàng)式來(lái)逼近有限區(qū)間內(nèi)的函數(shù),其一般形式為
f ( x ) = ∑ n = 0 N a n x n f(x)=\sum_{n=0}^N a_nx^n f(x)=n=0∑N?an?xn
其中最著名的應(yīng)該是自然指數(shù),根據(jù)其導(dǎo)數(shù)不變的特點(diǎn),我們可以很容易得到其表達(dá)式
e x = ∑ n = 0 N x n n ! e^x=\sum_{n=0}^N \frac{x^n}{n!} ex=n=0∑N?n!xn?
隨著N的不斷增加,其逼近過(guò)程如圖所示
其中,Taylor
級(jí)數(shù)的實(shí)現(xiàn)方法如下,除了exp
函數(shù)之外,還包括sin
和cos
函數(shù)的Taylor級(jí)數(shù)。
def Taylor(x,funcType='exp',n=0):
func = {
'exp' : lambda x,n : x**n/fac(n),
'sin' : lambda x,n : (-1)**n*x**(2*n+1)/fac(2*n+1),
'cos' : lambda x,n : (-1)**n*x**(2*n)/fac(2*n)
}
return func[funcType](x,n)
繪圖代碼如下
def approxGif(num=30, funcType='exp'):
funcType = func if type(func)==str else 'auto'
if type(func)==str:
func = funcDict[func]
x = np.linspace(0,10,1000)
Y =func if type(func)==type(x) else func(x)
if method in ['Taylor','taylor']:
y = Taylor(x,funcType,0)
elif method in ['Fourier','fourier']:
y = Fourier(x,funcType,0)
num = range(num)
#畫圖初始化
fig = plt.figure()
ax = fig.add_subplot(111,autoscale_on=False,
xlim=(0,10),ylim=(min(Y)-0.5,max(Y)+0.5))
ax.plot(x,Y,color='g',lw=0.2)
ax.grid()
line, = ax.plot([],[],lw=0.5)
time_text = ax.text(0.1,0.9,'',transform=ax.transAxes)
# 動(dòng)畫初始化
def init():
line.set_data([],[])
time_text.set_text('level:'+str(0))
return line, time_text
# 動(dòng)畫迭代
def animate(n):
nonlocal y
y = Taylor(x,funcType,n) if n==0 else y+Taylor(x,funcType,n)
line.set_data(x,y)
time_text.set_text('level:'+str(n))
print(n)
return line, time_text
ani = animation.FuncAnimation(fig,animate,
num,interval=200,blit=False,init_func=init)
#ani.save(funcType+'.gif',writer='pillow')
plt.show()
Fourier級(jí)數(shù)
Fourier級(jí)數(shù)也是本著相同的思維,只不過(guò)采用了不同頻率的三角函數(shù)作為其空間中的基底。
對(duì)于以 2 π 2\pi 2π為周期的方波信號(hào)
f ( x ) = { 1 x ∈ [ 0 , π ) ? 1 x ∈ [ ? π , 0 ) f(x)=\left\{\begin{aligned} 1\quad &x \in[0,\pi)\\ -1\quad &x\in[-\pi,0) \end{aligned}\right. f(x)={1?1?x∈[0,π)x∈[?π,0)?
其Fourier級(jí)數(shù)為
f ( x ) = 4 π ∑ n = 0 N sin ? x 2 n + 1 f(x)=\frac{4}{\pi}\sum_{n=0}^N\frac{\sin x}{2n+1} f(x)=π4?n=0∑N?2n+1sinx?
實(shí)現(xiàn)為
def Fourier(x,funcType='square',n=0):
func = {
'square' : lambda x,n : 4/np.pi*np.sin((2*n+1)*x)/(2*n+1),
'tri' : lambda x,n: np.pi/2 if n == 0 \
else -4/np.pi*np.cos((2*n-1)*x)/(2*n-1)**2,
'oblique': lambda x,n : 2*np.sin((n+1)*x)/(n+1)*(-1)**n
}
return func[funcType](x,n)
繪圖代碼為
def square(x):
x = np.mod(x,np.pi*2)
x[x>np.pi] = -1
x[x!=-1] = 1
return x
def tri(x):
return np.pi-np.abs(np.mod(x,2*np.pi)-np.pi)
funcDict = {
'exp':np.exp,
'sin':np.sin,
'cos':np.cos,
'square':square,
'tri':tri,
}
# func支持三種輸入模式,即字符串,函數(shù)以及numpy數(shù)組
def approxGif(func='square',method='fourier',num=30):
funcType = func if type(func)==str else 'auto'
if type(func)==str:
func = funcDict[func]
x = np.linspace(0,10,1000)
Y =func if type(func)==type(x) else func(x)
if method in ['Taylor','taylor']:
y = Taylor(x,funcType,0)
elif method in ['Fourier','fourier']:
y = Fourier(x,funcType,0)
num = range(num)
#畫圖初始化
fig = plt.figure()
ax = fig.add_subplot(111,autoscale_on=False,
xlim=(0,10),ylim=(min(Y)-0.5,max(Y)+0.5))
ax.plot(x,Y,color='g',lw=0.2)
ax.grid()
line, = ax.plot([],[],lw=0.5)
time_text = ax.text(0.1,0.9,'',transform=ax.transAxes)
# 動(dòng)畫初始化
def init():
line.set_data([],[])
time_text.set_text('level:'+str(0))
return line, time_text
# 動(dòng)畫迭代
def animate(n):
nonlocal y
if method in ['taylor','Taylor']:
y = Taylor(x,funcType,n) if n==0 \
else y+Taylor(x,funcType,n)
elif method in ['fourier','Fourier']:
y = Fourier(x,funcType,n) if n==0 \
else y+Fourier(x,funcType,n)
pass
line.set_data(x,y)
time_text.set_text('level:'+str(n))
print(n)
return line, time_text
ani = animation.FuncAnimation(fig,animate,
num,interval=200,blit=False,init_func=init)
#ani.save(funcType+'.gif',writer='pillow')
plt.show()
如圖所示
相應(yīng)地三角波為文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-655332.html
![]() |
|
![]() |
上述只是給出了幾個(gè)直觀的例子,用以表明Taylor級(jí)數(shù)和Fourier級(jí)數(shù)的使用方法,從而讓我們具備這種通過(guò)多項(xiàng)式或者三角函數(shù)來(lái)逼近已知函數(shù)的意識(shí)。而對(duì)于已知表達(dá)形式的函數(shù) y = f ( x ) y=f(x) y=f(x),Taylor級(jí)數(shù)和Fourier級(jí)數(shù)都有導(dǎo)數(shù)或者積分的表示形式。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-655332.html
到了這里,關(guān)于傅里葉級(jí)數(shù)和泰勒級(jí)數(shù)逼近已知函數(shù)的動(dòng)態(tài)過(guò)程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!