- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想绘制一个简单的图示,说明如何使用导数找出函数在任意点的斜率。它看起来有点像这样:
我已经使用这段代码绘制了一个简单的抛物线:
import numpy as np
from matplotlib import pyplot as plt
inputs = 0.2
weights = np.arange(-6,14)
target_prediction = 0.7
prediction = inputs*weights
errors = (prediction - target_prediction) ** 2
plt.xlabel("Weight")
plt.ylabel("Error")
plt.plot(weights, error)
现在我想添加这样的东西:
current_weight = 5
# draw a short fraction of a line to represent slope
x = np.arange(optimal_weight - 3, optimal_weight + 3)
# derivative
slope = 2 * (inputs*current_weight - target_prediction)
y = slope*x # How should this equation look like?
plt.plot(x, y)
绘制一条穿过 current_weight
的切线。
但我似乎无法弄清楚,你能帮忙吗?
最佳答案
一旦在所需点处获得斜率,就需要使用点斜率形式编写切线方程:
# Define parabola
def f(x):
return x**2
# Define parabola derivative
def slope(x):
return 2*x
# Define x data range for parabola
x = np.linspace(-5,5,100)
# Choose point to plot tangent line
x1 = -3
y1 = f(x1)
# Define tangent line
# y = m*(x - x1) + y1
def line(x, x1, y1):
return slope(x1)*(x - x1) + y1
# Define x data range for tangent line
xrange = np.linspace(x1-1, x1+1, 10)
# Plot the figure
plt.figure()
plt.plot(x, f(x))
plt.scatter(x1, y1, color='C1', s=50)
plt.plot(xrange, line(xrange, x1, y1), 'C1--', linewidth = 2)
您可以对任何可微函数执行此操作,并且可以使用导数近似方法(例如有限差分)来消除提供解析导数的需要。
关于python - 如何绘制任意一点抛物线的斜率(切线)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54961306/
试图找出在 Python 中返回直线斜率的函数。问题方向是用给定的斜率坐标求出 m 的斜率。通读其他几个堆栈溢出帖子,但似乎没有一个是可行的解决方案。以下是我尝试过的各种变体: def slope(x
我的代码中的所有内容终于看起来都是正确的。我只是遇到了一些棘手的问题。 如何编写代码,以便当我输入两个点且斜率为 -无穷大 时,它会被识别并且输出显示 Vertical 而不是 Negative坡度。
您可以通过此链接访问以下代码:jsfiddle.net/2NPxV 我的 CSS 代码: .custom_content { display: block; width:200px; height:
我需要计算一条线与水平线之间的角度。我的高中数学似乎不及格。 import matplotlib.pyplot as plt import numpy as np x = [8450.0, 8061.
我有一个 Python 程序,可以显示温度下降与时间的关系图。沿着下降过程,温度在一段时间内保持恒定,几乎为 0 斜率,然后继续下降。当温度恒定时,曲线中的这个区域我希望程序能够自动检测并显示 y 值
如何使用 R 将 RMSE、斜率、截距和 r^2 添加到绘图中?我附加了一个包含示例数据的脚本,它的格式与我的真实数据集类似 - 不幸的是,我处于停滞状态。有没有比从方程创建对象并将其插入到 text
我是一名优秀的程序员,十分优秀!