Issue
Currently implementing a momentum gradient descent, but I need to find the y value at the specific points x = 2.0000000052746745 for the first plot and x = 3.000000003516446 for the second plot
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)
Solution
You need some handler to get data from axes:
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)
Further, if you need some interpolation over data you can then use numpy interpolation through griddata
[link].
Answered By - Radek D
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.