一阶导数计算

x=np.arange(1,100)
y = np.sin(0.1*x)

def D_1(x,y):
	n = len(x)
	d = np.zeros(n)
	d[0] = (y[1]-y[0])/(x[1]-x[0])
	d[n-1] = (y[n-1]-y[n-2])/(x[n-1]-x[n-2])
	for i in range(1,n-1):
		d[i] = (((y[i+1]-y[i])/(x[i+1]-x[i])+(y[i]-y[i-1])/(x[i]-x[i-1])))/2
	return d


d = 10*D_1(x,y)
y1 = np.cos(0.1*x)


fig,ax=plt.subplots()
ax.plot(x,y,'b-',label='sin(x)')
ax.plot(x,d,'r-',label='derivative')
ax.plot(x,y1,'g-.',label='cos(x)')
ax.legend(loc=1)
plt.show()

  

 

posted @ 2022-03-29 16:10  华小电  阅读(387)  评论(0编辑  收藏  举报