I am trying to fit an ellipse to the end of a curve, $y(x)$, such that first and second derivatives of the curve, $\frac{dy}{dx}$ and $\frac{d^2y}{dx^2}$, are preserved at the contact point, $(x,y)$, and the ellipse has its centre on the $x$-axis.
For an ellipse of the form: $$ \frac{(x-x_0)^2}{a^2} + \frac{(y-y_0)^2}{b^2} = 1 $$
I have $y_0 = 0$, so that the centre is on the $x$-axis, and
$$ x_0 = x - \frac{(y-y_0)\frac{dy}{dx}}{(y-y_0)\frac{d^2y}{dx^2}+\left(\frac{dy}{dx}\right)^2} $$
$$ a = \pm \left((x-x_0)^2 - \frac{(x-x_0)(y-y_0)}{\frac{dy}{dx}}\right)^{1/2} $$
$$ b = \pm\frac{a(y-y_0)}{\left( a^2 - (x-x_0)^2 \right)^{1/2}}$$
where the above equations have been obtained by following the algebra through (hopefully correctly).
When I apply this to some examples (see below) I appear to get complex results (for $b$), where it looks as if there should be a real result. What is the reason for this? Does a real solution exist?
E.g. Fit ellipse to polynomial: $$ y = \alpha x^2 + \beta x + \gamma $$ where $\alpha = -0.14$, $\beta = 4.37$ and $\gamma = -28.8$, at $x=11.9$.
This gives $y=3.38$, $\frac{dy}{dx}=1.04$ and $\frac{d^2y}{dx^2}=-0.28$ at the contact point. Using the equations above the ellipse parameters come out as $x_0 = -14.72$, $a = 24.94$ and $b = -9.05i$.
Differentiating two times the equation of the ellipse leads to the quations which are solved as shown in attachment.