Radius of largest circle which fits the curve in Python

Viewed 129

How do I find the radius of the largest circle which fits the following curve, y=sin(x)?

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,1.01*np.pi,0.01)   # start,stop,step
y = np.sin(x)
plt.plot(x,y)
plt.show()

enter image description here

1 Answers

You can solve this problem analytically using a basic maths (parametric) equation system. Indeed, the circle can be described by the algebraic expression x² + y² ≤ r². The one of the shape below your curve is 0 ≤ y ≤ sin(x). The definition domain of x is [0,π[ (you can find it analytically or numerically using the Newton's method). Then you search to maximize r.

You can quickly see that the biggest circle will "touch" the abscissa axis as well as the curve. Since a circle can be defined using only 3 points, then you only need to find the 2 that are on the curve but the best solution requires the two point to be in the same position and the circle to be centred in the domain. Tools like Geogebra or Wolfram Alpha are good to easily solve such analytic equation system. Here is an example:

Best solution

The best solution is r=0.5.

Assuming the curve have a complex definition (possibly not an analytic one), you can solve this problem using optimization methods. For example, you can use scipy.optimize.

Related