# python计算导数并绘图的实例

```import math
import numpy as np
import matplotlib.pyplot as plt
from sympy import * #用于求导积分等科学计算

def dif(left,right,step):#求导 左右区间以及间隔
x,y = symbols('x y')#引入x y变量
expr = pow(x,5)#计算表达式
x_value = [] #save x value
y_value = [] #save x f(x) value
y_value_dif = [] #save x f(x)_dot value
y_value_dif2 = [] #save x f(x)_dot2 value
y_value_dif3 = [] #save x f(x)_dot3 value
y_value_dif4 = [] #save x f(x)_dot4 value
#print(expand(exp(I*x), complex=True))#将复指数展开成实部虚部形式
expr_dif = diff(expr,x,1)
expr_dif2 = diff(expr,x,2)
expr_dif3 = diff(expr,x,3)
expr_dif4 = diff(expr,x,4)
for i in np.arange(left,right,step):
x_value.append(i)
y_value.append(expr.subs('x',i))#将i值代入表达式
y_value_dif.append(expr_dif.subs('x',i))#将i值代入求导表达式
y_value_dif2.append(expr_dif2.subs('x',i))#将i值代入2阶求导表达式
y_value_dif3.append(expr_dif3.subs('x',i))#将i值代入3阶求导表达式
y_value_dif4.append(expr_dif4.subs('x',i))#将i值代入4阶求导表达式
draw_plot_set()#设置画图格式
plt.plot(x_value,y_value,"b-",linewidth=1,label='f(x)='+str(expr)) #画图
plt.plot(x_value,y_value_dif,"r-",linewidth=1,label='f(x)_prim') #画图
plt.plot(x_value,y_value_dif2,"y-",linewidth=1,label='f(x)_prim2') #画图
plt.plot(x_value,y_value_dif3,"g-",linewidth=1,label='f(x)_prim3') #画图
plt.plot(x_value,y_value_dif4,"b-",linewidth=1,label='f(x)_prim4') #画图

plt.legend()#显示图例
plt.show()#显示图像

def draw_plot_set():#设置画图格式
plt.figure()
ax = plt.gca()
#改变坐标轴位置
ax.spines['right'].set_color('none')#删除原来轴
ax.spines['top'].set_color('none')#删除原来轴
ax.xaxis.set_ticks_position('bottom')#在0点处增加轴
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')#在0点处增加轴
ax.spines['left'].set_position(('data',0))
#设置坐标名
plt.ylabel('f(x)')
plt.xlabel('x')

plt.grid(True)#打开网格

if __name__ == '__main__':
dif(-5,5,0.01)
```

```import sympy as sp
import numpy as np
x,y = sp.symbols('x y')
z = sp.sin(2*sp.pi*x+2*y/5)
zx = sp.diff(z,x)
zy = sp.diff(z,y)
print(zx)
print(zy)```

```2*pi*cos(2*pi*x + 2*y/5)
2*cos(2*pi*x + 2*y/5)/5```

```import sympy as sp
import numpy as np
x,y = sp.symbols('x y')
z = sp.sin(2*sp.pi*x+2*y/5)
zx = sp.diff(z,x)
zy = sp.diff(z,y)
x1 = 10
y1 = 5
z_x1 = float(zx.evalf(subs={x:x1,y:y1}))
z_y1 = float(zy.evalf(subs={x:x1,y:y1}))
print(z_x1)
print(z_y1)```

```-2.61472768902227
-0.16645873461885696```

```import sympy as sp
import numpy as np
x,y = sp.symbols('x y')
z = sp.sin(2*sp.pi*x+2*y/5)
zx = sp.diff(z,x)
zy = sp.diff(z,y)
x_array = np.linspace(-5, 5, 10)
y_array = np.linspace(-5, 5, 10)
temp_x = []#先定义一个用于存储x偏导的空列表
temp_y = []#先定义一个用于存储y偏导的空列表
for i in range(10):
z_x = float(zx.evalf(subs={x:x_array[i],y:y_array[i]}))
temp_x.append(z_x)#将计算得到的偏导值一一添加到列表中
z_y = float(zy.evalf(subs={x:x_array[i],y:y_array[i]}))
temp_y.append(z_y)
zx_array = np.array(temp_x)#将列表转换为数组
zy_array = np.array(temp_y)
print(zx_array)
print(zy_array)```

```[-2.61472769 4.11163864 6.02946289 0.89585862 -5.2854481 -5.2854481
0.89585862 6.02946289 4.11163864 -2.61472769]
[-0.16645873 0.26175505 0.38384753 0.05703213 -0.33648208 -0.33648208
0.05703213 0.38384753 0.26175505 -0.16645873]```