如何在给定图中指定的 Y 值的情况下找到相应的 X 值

How to find corresponding X value given a specified Y value in a plot

目前正在实施动量梯度下降,但我需要找到第一个图的特定点 x = 2.0000000052746745 和第二个图的 x = 3.000000003516446 处的 y 值

def dz_dx(x,y):
    return (x-2)/(np.sqrt(25-(x-2)**2-(y-3)**2))

def dz_dy(x,y):
    return (y-3)/(np.sqrt(25-(x-2)**2-(y-3)**2))

xStart = 5
yStart = 5
learning_rate = 0.01
maxLimit = 10000
xStartHistory = np.zeros(maxLimit)
yStartHistory = np.zeros(maxLimit)
gamma = 0.9
update1 = 0
update2 = 0

for i in range(maxLimit):
    xStartHistory[i] = xStart
    yStartHistory[i] = yStart

    dx = dz_dx(xStart, yStart)
    dy = dz_dy(xStart, yStart)

    update1 = (gamma * update1) + (learning_rate * dx)
    update2 = (gamma * update2) + (learning_rate * dy)
    xStart = xStart - update1
    yStart = yStart - update2

print("xHistory:",xStartHistory[maxLimit-1])
print("yHistory:",yStartHistory[maxLimit-1])

figs, axs = plt.subplots(2)
axs[0].plot(xStartHistory)
axs[1].plot(yStartHistory)

您需要一些处理程序来从坐标区获取数据:

line0, = axs[0].plot(xStartHistory)
line1, = axs[1].plot(yStartHistory)

datax0 = line0.get_xdata()
datay0 = line0.get_ydata()

# value of y at x=2.000:
y_at_x0 = datay0[list(datax0).index(2.000)]

datax1 = line1.get_xdata()
datay1 = line1.get_ydata()
# value of y at x=3.000:
y_at_x1 = datay1[list(datax1).index(3.000)]

print(y_at_x0)
print(y_at_x1)

此外,如果您需要对数据进行一些插值,则可以通过 griddata [link].

使用 numpy 插值