一阶导数计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 @   华小电  阅读(440)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示