# Finding & Plotting equation of hyperbola given foci, and difference in distances between them.

I have to plot the hyperbola (3 of them actually) in MATLAB, and so it'd be good if I could find some sort of general formula.

The foci do not necessarily have to be on the axes (e.g. $(5,3)$ and $(4,8)$ can also be the foci). I have the difference in distances between them at a point which lies on the hyperbola. Will there be a general equation describing this hyperbola? If yes, what will it be? (e.g. say the points are $(x_1,y_1)$ & $(x_2,y_2)$ and difference in distance is $d$).

I know that I can shift and rotate the axes so that I get what I want, but I wanted to know if there was a better method, since while plotting i'll have to reconvert the values into the normal axes.

I'm going to try trilateration, which is why i'm asking for the equation.

-
The answers to this question math.stackexchange.com/questions/31756/… provide what you are looking for, I think. – Ellie Kesselman Jan 22 '12 at 15:16
Actually, one of the answers specifically addresses the case where the foci are not on the axes, and just about everything else possible for a hyperbola. It is a remarkably fine answer math.stackexchange.com/a/31761/9260 – Ellie Kesselman Jan 22 '12 at 15:21
@FeralOink - Thanks! :) – Khushman Patel Jan 22 '12 at 17:16
The equation of the hyperbola is an implicit one. Can't you plot implicit equations? – Américo Tavares Jan 22 '12 at 22:54
Simply put, if you need to use parametric equations as opposed to an implicit Cartesian equation, rotate/shift is really the only way to go. Otherwise, if your computing environment supports implicit plotting, then even the obvious approach of using the distance formula works nicely. – J. M. Jul 24 '12 at 21:49

say the points are $(x_1,y_1)$ & $(x_2,y_2)$ and difference in distance is $d$

Could you use the literal translation of this statement into algebra? $$\left|\sqrt{(x-x_1)^2+(y-y_1)^2}-\sqrt{(x-x_2)^2+(y-y_2)^2}\right|=d$$ or $$\left(\sqrt{(x-x_1)^2+(y-y_1)^2}-\sqrt{(x-x_2)^2+(y-y_2)^2}\right)^2=d^2$$ or $$(x-x_1)^2+(y-y_1)^2+2\sqrt{(x-x_1)^2+(y-y_1)^2}\sqrt{(x-x_2)^2+(y-y_2)^2}+(x-x_2)^2+(y-y_2)^2=d^2$$

-
Typically one does $\sqrt{(x-x_1)^2+(y-y_1)^2} = \sqrt{(x-x_2)^2+(y-y_2)^2} \pm d$, so that (after squaring both sides, simplifying, and squaring again) one gets a quadratic equation, not a quartic one. – Rahul Feb 20 '12 at 8:42
@ℝⁿ. - Taking the +d case, squaring both sides gives $$(x-x_1)^2 + (y-y_1)^2 = (x-x_2)^2 + (y-y_2)^2 + 2d\sqrt((x-x_2)^2 + (y-y_2)^2) + d^2$$. Only a little bit of simplification seems possible: $$-2xx_1 + x_1^2 -2yy_1 + y_1^2 = -2xx_2 + x_2^2 -2yy_2 + y_2^2 + 2d\sqrt((x-x_2)^2 + (y-y_2)^2) + d^2$$. Now from here if you move everything but the $$2d\sqrt((x-x_2)^2 + (y-y_2)^2)$$ to the left side so you can square both sides again to get rid of the sqrt, I count 9 additive terms on the left that all have to be multiplied by each other... is that really what needs to be done? – David Doria Feb 20 '13 at 18:36
@David: Well, you ought to group them into the form $ax+by+c$ before squaring. – Rahul Feb 20 '13 at 18:42
@ℝⁿ. See my answer below - any more suggestions for improvement :)? – David Doria Feb 20 '13 at 20:57

Perhaps someone can verify the result below, or suggest cleaner ways to expand/write this?

Starting from the definition of a hyperbola as the locus of points X = (x,y) that have a constant distance difference to two points (the foci, A = (a_x, a_y) and B = (b_x, b_y)): $$|A-X| - |B-X| = d$$

We can write this (at least half of the solution) as:

$$\sqrt{(a_x-x)^2 + (a_y-y)^2} - \sqrt{(b_x-x)^2 + (b_y-y)^2} = d$$

There is tremendous manipulation necessary to write this equation in general quadratic form ($Ax^2 + By^2 + Cxy + Dx + Ey + F = 0$):

Move one of the square roots to the other side of the equation so that when we square both sides we will not eventually introduce quartic terms: $$\sqrt{(a_x-x)^2 + (a_y-y)^2} = \sqrt{(b_x-x)^2 + (b_y-y)^2} + d$$

Square both sides: $$(a_x-x)^2 + (a_y-y)^2 = (b_x-x)^2 + (b_y-y)^2 + 2d \sqrt{(b_x-x)^2 + (b_y-y)^2} + d^2$$

Expand: $$a_x^2 -2xa_x + x^2 + a_y^2 - 2a_y y + y^2 = b_x^2 - 2xb_x + x^2 + b_y^2 -2b_y y + y^2 + 2d \sqrt{(b_x-x)^2 + (b_y-y)^2} + d^2$$

Cancel $x$ and $y$ quadratic terms (that occur on both sides of the equation): $$a_x^2 -2xa_x + a_y^2 - 2a_y y = b_x^2 - 2xb_x + b_y^2 -2b_y y + 2d \sqrt{(b_x-x)^2 + (b_y-y)^2} + d^2$$

Move everything not connect to the square root to the left side so we can eventually square both sides again to get rid of the square root: $$a_x^2 - 2xa_x + a_y^2 - 2a_y y - b_x^2 = 2xb_x - b_y^2 + 2b_y y - d^2 = 2d \sqrt{(b_x-x)^2 + (b_y-y)^2}$$

Group terms on the left side into the form Jx + Ky + L to make squaring easier: $$\left((-2a_x + 2b_x)x + (-2a_y + 2b_y)y + (a_x^2 + a_y^2 - b_x^2 - b_y^2 - d^2)\right) = 2d \sqrt{(b_x-x)^2 + (b_y-y)^2}$$

Assign $J = -2a_x + 2b_x$, $k = -2a_y + 2b_y$, $L = a_x^2 + a_y^2 - b_x^2 - b_y^2 - d^2$ so this can be written as

$$Jx + Ky + L = 2d \sqrt{(b_x-x)^2 + (b_y-y)^2}$$

Square both sides:

$$(Jx + Ky + L)^2 = 4d^2 \left((b_x-x)^2 + (b_y-y)^2\right)$$

$$J^2x^2 + JxKy + JxL + KyJx + K^2y^2 + KyL + LJx + LKy + L^2 = 4d^2 \left(b_x^2 - 2xb_x +x^2 + b_y^2 - 2yb_y + y^2\right)$$

Combine terms on the left, and multiply through on the right:

$$J^2x^2 + K^2y^2 + 2JxKy + 2JxL + 2KyL + L^2 = 4d^2 b_x^2 - 8d^2xb_x + 4d^2x^2 + 4d^2b_y^2 - 8d^2yb_y + 4d^2y^2$$

Combine all terms ($x^2, y^2, xy, x, y$):

$$(J^2-4d^2)x^2 + (K^2-4d^2)y^2 + (2JK)xy + (2JL+8d^2b_x)x + (2KL + 8d^2b_y)y + (L^2 - 4d^2 b_x^2 - 4d^2 b_y^2) = 0$$

Substituting back in the values of J, K, and L:

$$((-2a_x + 2b_x)^2-4d^2)x^2 + ((-2a_y + 2b_y)^2-4d^2)y^2 + (2(-2a_x + 2b_x)(-2a_y + 2b_y))xy \\+ (2(-2a_x + 2b_x)(a_x^2 + a_y^2 - b_x^2 - b_y^2 - d^2)+8d^2b_x)x + (2(-2a_y + 2b_y)(a_x^2 + a_y^2 - b_x^2 - b_y^2 - d^2) + 8d^2b_y)y \\+ ((a_x^2 + a_y^2 - b_x^2 - b_y^2 - d^2)^2 - 4d^2 b_x^2 - 4d^2 b_y^2) = 0$$

Expand: $$(4a_x^2 - 8a_x b_x + 4b_x^2)x^2 + \\ (4a_y^2 - 8a_y b_y + 4b_y^2)y^2 + \\ (8a_xa_y - 8a_xb_y - 8a_yb_x + 8b_xb_y)xy +\\ (-4a_x^3 -4a_xa_y^2 + 4 a_x b_x^2 + 4 a_x b_y^2 + 4 a_x d^2 + 4b_xa_x^2 + 4b_xa_y^2 + 4b_x^3 - 4b_xb_y^2 + 4b_xd^2)x +\\ (-4a_ya_x^2 - 4a_y^3 + 4 a_y b_x^2 + 4 a_y b_y^2 + 4 a_y d^2 + 4b_ya_x^2 + 4b_ya_y^2 - 4b_yb_x^2 - 4b_y^3 + 4b_yd^2)y +\\ a_x^4 + a_x^2 a_y^2 - a_x^2b_x^2 - a_x^2b_y^2 - a_x^2d^2\\ + a_y^2 a_x^2 + a_y^4 - a_y^2b_x^2 - a_y^2b_y^2 - a_y^2 d^2\\ - b_x^2 a_x^2 - b_x^2 a_y^2 + b_x^4 + b_x^2 b_y^2 -3 b_x^2 d_2\\ - b_y^2 a_x^2 - b_y^2a_y^2 + b_y^2b_x^2 + b_y^4 + b_y^2 d^2\\ - d^2 a_x^2 - d^2 a_y^2 + d^2 b_x^2 -3 d^2 b_y^2 + d^4\\ = 0$$

-
A slightly cleaner way would be to keep everything in vector notation. $\sqrt{\lVert\mathbf a-\mathbf x\rVert^2} = \sqrt{\lVert\mathbf b-\mathbf x\rVert^2} + d$, $\lVert\mathbf a\rVert^2-2\mathbf a\cdot\mathbf x+\lVert\mathbf x\rVert^2=\lVert\mathbf b\rVert^2-2\mathbf b\cdot\mathbf x+\lVert\mathbf x\rVert^2+2d\sqrt{\lVert\mathbf b-\mathbf x\rVert^2}+d^2$, and so on. This has the additional advantage that the derivation doesn't change at all in higher dimensions. – Rahul Mar 6 '13 at 11:33
In this derivation we assumed |A−X|>|B−X|. Even though we only solved "half" of the absolute value equation given by the definition of the hyperbola, we still seem to obtain the entire hyperbola. What is the relation of the object obtained if |B−X|>|A−X| to the one we derived above? – David Doria Mar 18 '13 at 17:04