You are NOT finding the perpendicular distance. You are finding the minimum of the y-values with fixed x values. Your steps need to look like this:
- find all perpendicular lines to one of the curves.
- find the corresponding intersection point on the other curve.
- calculate the distance.
Note that this method is not commutative, the perpendicular distance from f to g may not be the same as the perpendicular distance from g to f. This is because you are not guaranteed that if a line is perpendicular to curve f that it must also be perpendicular to curve g.
Some math:
- the derivative of F is 1
- the derivative of G is 2*X
- the perpendicular line of Y=X+1 at a point x is Y=-X+(2*x+1).
- the perpendicular line of Y=X**2 at a point x is Y=-1/(2*x)*X+(x**2+0.5)
- the intersection points could have more than one solution
I will show in numpy.
import numpy as np
#Functions, d is the derivative
F = lambda x : x+1
dF = lambda x : np.ones(x.shape)
G = lambda x : x**2
dG = lambda x : 2*x
#Domain
X = np.arange(1,5,1)
#We calculate the distance from G to F
P_m = -1/dG(X) #Perpendicular slopes
P_b = X**2+0.5 #The y-intercept
C = (2*X**3-X)/(2*X+1) #The x-coor of the intersection
D = np.sqrt((X-C)**2+((P_m*X+P_b)-F(C))**2) #Distance
print(D.min())
#Now the other way (this way has two intersection points).
P_m = -1/dF(X) #The perpendicular slopes
P_b = 2*X+1 #The y-intercepts
C1 = 0.5*(-P_m+np.sqrt(P_m**2+4*P_b)) #First solution
C2 = 0.5*(-P_m-np.sqrt(P_m**2+4*P_b)) #Second solution
D1 = np.sqrt((X-C1)**2+((P_m*X+P_b)-G(C1))**2) #Euclidian distance to first solution
D2 = np.sqrt((X-C2)**2+((P_m*X+P_b)-G(C2))**2) #Euclidian distance to second solution
D = np.concatenate([D1,D2]) #Distance
print(D.min()) #Minimum distance
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…