def find_closest(t, v, t0, v0):
""" Find the closest point on the curve f = a + b/x
to the given point (t,v)
"""
a = v0
b = v0*t0
# Solve for intersection points
eqn_coefs = [1/b, -t/b, 0, v-a, -b]
tis = np.roots(eqn_coefs)
tis = tis[abs(tis.imag/tis.real)<0.01].real # We care only real solutions
tis = tis[tis>0] # and positive ones
# Choose the shortest among solutions
ds = abs(tis-t)*np.sqrt(1 + np.power(tis,4)/(b*b)) # Distance from solutions to given point (t,v)
idx = np.argmin(ds)
ti = tis[idx]
vi = a + b/ti
return ti, vi
评论列表
文章目录