Minimum Distance to f(x) with Scipy Optimize
Shortest Distance Between f(x) and a Point ¶ In this notebook, I show a simple demonstration of using Scipy Optimize to find shortest distance from a given point $(𝑥_p,𝑦_p)$ to a curve: 𝑦 = 𝑓(𝑥). I will need to import a few Python libraries for this. In [1]: import numpy as np import scipy.optimize as optimize import matplotlib.pyplot as plt Scipy Optimize has an impressive array of functions to support common optimization problems. For this problem, I will use scipy.minimize to find the minimum distance. minimize requires an objective function and an initial guess for $x_{opt}$. f(x) is the "curve" we want to find the shortest distance to. This can be any continuous function of x. In [2]: def f ( x ): return np . cos ( x ) # + np.sin(x) def dist ( x ): """ Returns the Euclidean distance from (xp, yp) to (x, f(x)) """ y = f ( x ) dist ...