1、绘制函数𝑦=𝑓(𝑥)=𝑥3−1𝑥y=f(x)=x3−1x和其在𝑥=1x=1处切线的图像
import numpy as np import matplotlib.pyplot as plt # 中心差分 def numerical_diff(f, x): h = 1e-4 return (f(x + h) - f(x - h)) / (2 * h) def function_1(x): return x**3 - 1/x # x=1处的切线方程 def tangent_line(f, x): d = numerical_diff(f, x) print(d) y = f(x) - d * x return lambda t: d * t + y x = np.arange(0.5, 3, 0.01) y = function_1(x) plt.xlabel("x") plt.ylabel("y") z = tangent_line(function_1, 1) y2 = z(x) plt.plot(x, y) plt.plot(x, y2) plt.show()
def 函数名([参数,参数,......]):函数体
d=numerical_diff(f,x):d调用numerical_diff求在x点的导数
y=f(x)-d*x:y为截距,d为斜率(y=kx+b)
numpy.arange(start,stop,step,dtype):使用arange函数创建数值范围并返回ndarray对象
start与stop为指定的范围,step为步长
plt.xlabel():表示x轴
plt.ylabel():表示y轴
plt.plot(x,y):是matplotlib.pyplot模块下的一个函数,用于画图。x为x轴数据,y为y轴数据
梯度:全部变量的偏导数汇总而成的向量
2、求函数𝑓(𝐱)=3
+5
的梯度。
3、函数𝑓(𝐱)=
的梯度是什么?
4、你可以写出函数𝑢=𝑓(𝑥,𝑦,𝑧),其中𝑥=𝑥(𝑎,𝑏),𝑦=𝑦(𝑎,𝑏),𝑧=𝑧(𝑎,𝑏)的链式法则吗?