I'm trying to make a rather complex fit to a set of data points in python, and after much googling and reading I still have no idea where to start.The data are (x, y) points that look like this:
I want to approximate them by a shape formed by two circular caps (see below), defined by
x^2 + (y-c_alpha)^2 = R_alpha^2 for y < 0
(grey)
x^2 + (y-c_beta)^2 = R_beta^2 for y < 0
(green)
with the constraint
R_alpha^2 = R_beta^2 + (c_alpha-c_beta)^2 - 2*(c_alpha-c_beta)*R_beta*cos(sigma)
,
where cos(sigma) = -c_beta/R_beta
(note that c_beta < 0
), which ensures that the two caps are connected. Hence, there are three independent variables. I would like to find the values of R_beta
, c_beta
and R_alpha
, for example, that best represent the data.
The idea doesn't seem too complicated, but the implementation has several difficulties that I don't know how to tackle:
- The function to fit must be defined implicitly, as writing
y(x) = sqrt(...)
for the green cap would exclude part of the data. - It has to be somehow defined piecewise, so it knows to fit the grey data with the grey cap and the green data with the green cap. Or do I have to fit each cap separately and then somehow enforce the constraint? How would that be?
Any hint is of great help, and of course pieces of code are greatly appreciated. Thanks in advance!