How To Fit An Ellipse to Data
[mathematics
]
Last month, Karen Kikuchi, a matheamtics teacher at Rancho Campana High School in Camarillo, CA, reached out to the Mathematics Department at CI to ask if there were a mathematician who could advise a group of students on a mathematics problem they were wrestling with. The students were working on submitting an entry to NASA’s App Development Challenge.
Here’s the problem statement as she shared it with me.
We know that given two points with attached velocity vectors iin space, the smooth curve that connects the two will be an arc of an ellipse. The question is, what is the equation of this ellipse? (Note: there are infinitely many such ellipses, we desire the ellipse with the lowest ecentricity/)
She also referenced a post in Math StackExchange that answers the question, but the students were having trouble understanding the answer. She also shared a Desmos rendering of the data they are working with.
Reviewing the problem and the links confused me; I didn’t see the obvious question between what they were asking and the NASA challenge. So I did my best, starting with translating the StackExchange post into a narrative that I thought the students (who had completed an AP Calculus BC course) could understand. I also built a couple Desmos models to demonstrate the utility of the mathematics from a coding perspective.
This posting memorializes this work.
First, it’s worth making the problem statement more precise. Given two points and a line through each point, find the equation of an ellipse passing through the points with the lines tangent at those points. There will be an infinite number of such ellipses, so we are interested in finding the ellipse with minimal eccentricity.
Let $P_1$ and $P_2$ be points in $\mathbb{R}^2$ and let $\ell_i$ be a line through $\P_i$. We are interested in finding the equation of the ellipses that pass through these points and have the $\ell_i$ as tangent line at $P_i$. There are infinitely many such ellipses, so we will conclude with an observation on finding that ellipse with least eccentricity.
There are two cases to consider: first, the case where the $\ell_i$ intersect and, second, the case that the $\ell_i$ are parallel. We will consider the former.
Denote the point of intersection of the $\ell_i$ as $O$. Without loss of generality, we can assume $O$ sits at the origin. Define vectors $\vec{p}_i=\overrightarrow{OP_i}$, the vector from the origin to the point $\P_i$. Since the $\ell_i$ are not parallel lines, the cevtors $\vec{p}_i$ are linearly independent, so they form a basis for the plane. This means each point $\vec{x}$ in the plane has a coordinate representation with respect to ${\vec{p}_1,\vec{p}_2}$, written $\vec{x}=[u,v]^T$ meaning $u$ and $v$ are real numbers such that $\vec{x}=u\vec{p}_1+v\vec{p}_2$.
In this coordinate system, our ellipses have the form [ (u-1)^2+(v-1)^2+2 \alpha u v =1 ] where $\alpha$ is a real number with $|\alpha|<1$. You can see a model of such a family of ellipses rendered in Desmos here. (Ignore the ‘center’ for now. Where it comes from will become clear.)
Next, we locate the center of the ellipse. Let $\vec{q}_1$ and $\vec{q}_2$ be the basis dual to our basis ${\vec{p}_1,\vec{p}_2}$ for the plane. This means that
\[\vec{p}_i \cdot \vec{q}_j = \delta_{ij} = \begin{cases} 0 & \, \mbox{if } i \neq j \\ 1 & \, \mbox{if } i=j. \end{cases}\]Consequently, if $\vec{x}=u\vec{p}_1+v\vec{p}_2$, then
\[u=\vec{x} \cdot \vec{q}_1 \mbox{ and } v=\vec{x} \cdot \vec{q}_2.\]Using this, our equation for the ellipses becomes
\[(\vec{x} \cdot \vec{q}_1-1)^2+(\vec{x} \cdot \vec{q}_2)^2 + 2 \alpha (\vec{x} \cdot \vec{q}_1)(\vec{x} \cdot \vec{1}_1) =1.\]Next, we use the fact that the center of such an ellipse is the critical point of the expression on the left-hand side (LHS) of the above. The gradient of the LHS is
\(\vec{q}_1 (\vec{x} \cdot \vec{q}_1+\alpha \vec{x} \cdot \vec{q}_2-1)+\vec{q}_2 (\vec{x} \cdot \vec{q}_2+\alpha \vec{x} \cdot \vec{q}_1-1).\) Since ${\vec{q}_1,\vec{q}_2}$ form a basis, they are linearly independent. Therefore the above expression vanishes if and only if
\[\vec{x} \cdot \vec{q}_i+\alpha \vec{x} \cdot \vec{q}_j-1=0\]for $i\neq j$. Notice that $\vec{x} \cdot \vec{q}_1=\vec{x} \cdot \vec{q}_2 =\frac{1}{1+\alpha}$ solves both equations. This means the critical point of the ellipse equation is
\[\begin{align*} \vec{x} & = \frac{1}{1+\alpha} \vec{p}_1 + \frac{1}{1+\alpha} \vec{p}_2 \\ & = [\frac{1}{1+\alpha},\frac{1}{1+\alpha}] \end{align*}\]where the latter is the expression of the former in $\vec{p}_1\vec{p}_2-$coordinates. This gives is the center of the ellipse described by our first equation, above. (And this explains that addition in the Desmos notebook shared above.)