# What are some more efficient and better methods to solve for the equation of an ellipse?

Given that point $$A(0,3)$$ and point $$P(3,\frac{3}{2})$$ are respectively on the ellipse $$C:\frac{x^{2}}{a^{2}}+\frac{y^{2}}{b^{2}}=1(a>b>0)$$,

To find the equation of this ellipse.

Method One:

sol = First@
Solve[{x^2/a^2 + y^2/b^2 == 1 /. {x -> 0, y -> 3},
x^2/a^2 + y^2/b^2 == 1 /. {x -> 3, y -> 3/2}}, {a, b},
Assumptions -> {a > 0, b > 0}]
x^2/a^2 + y^2/b^2 == 1 /. sol


Method Two:

sol = First@
Solve[{x^2/a^2 + y^2/b^2 == 1 /. Thread[{x, y} -> {0 , 3}],
x^2/a^2 + y^2/b^2 == 1 /. Thread[{x, y} -> {3, 3/2}]}, {a, b},
Assumptions -> {a > 0, b > 0}]
x^2/a^2 + y^2/b^2 == 1 /. sol


What are some more efficient and better methods to solve for the equation of an ellipse?

I don't know why "efficient" or "better" in this case. Sometimes they're put in to make the question appear more interesting and less closable.

Here is a cool solution. Don't know whether it's efficient or better.

First@GroebnerBasis[
x^2/a^2 + y^2/b^2 ==
1 /. { {eq_ :> eq}, {x -> 0, y -> 3}, {x -> 3, y -> 3/2}}
, {x, y}, {a, b}] == 0 // (* since a certain form is desired... *)
Simplify //
DivideSides[#, Last[#]] & //
Expand

(*  x^2/12 + y^2/9 == 1  *)


For efficiency, one should solve numerically, not symbolically. Here's a start. Maybe others can show there are more efficient ways. I'm going hiking. Cya.

coeff = Table[
Block[{x, y}, {x, y} = p; {x^2, y^2, -1}], {p, {{0, 3}, {3, 3/2}}}];
Thread[{a, b} -> 1/Sqrt[Most[#]/Last[#] &@ First@NullSpace@coeff]]

(*  {a -> 2 Sqrt[3], b -> 3}  *)


Shorter form of OP's entirely adequate solution:

Last@Solve[
x^2/a^2 + y^2/b^2 == 1 /.
{{x -> 0, y -> 3}, {x -> 3, y -> 3/2}}, {a, b}]

(*  {a -> 2 Sqrt[3], b -> 3}  *)

• Wonderful variety: enjoy the hike +1:) Commented Jul 10 at 20:30

I prefer to use smaller commands, one after the other. It is more clear and reduces the chance of making errors.

eq = x^2/a^2 + y^2/b^2 == 1
pts = {{0, 3}, {3, 3/2}}


And now

eqs = Map[eq /. {x -> #[[1]], y -> #[[2]]} &, pts]
sol = Solve[eqs, {a, b}, Assumptions -> {a > 0, b > 0}]


Plug the solution back into the equation

eq /. sol


Ofcourse, since this is Mathematica, there are at least 10 different ways to code this. So just pick one style that is clear to you.

This is much more important than worrying about few nano seconds of efficiency.

• Of course, since we're working with Mathematica, there are at least 10 different ways to approach coding a solution. So, just choose a style that makes sense to you and is clear. This is much more important than worrying about a few nanoseconds of efficiency. Writing code from different perspectives can help me become more familiar with the relationships between function statements, much like solving a problem with multiple methods. This is also a way to learn Mathematica effectively. Commented Jul 10 at 13:24

Just a variant:

f[{x_, y_}] := {x, y} . DiagonalMatrix[{u, v}] . {x, y}
p = {{0, 3}, {3, 3/2}};
sol = Solve[Thread[f /@ p == 1], {u, v}][[1]];
ellipse = f[{x, y}] == 1 /. sol
{a, b} = {1/Sqrt[u], 1/Sqrt[v]} /. sol
foci = {Sqrt[a^2 - b^2], 0}
d = Norm[foci - #] + Norm[-foci - #] & /@ p // FullSimplify
ContourPlot[ellipse, {x, -4, 4}, {y, -4, 4},
Epilog -> {Red, Point[p], Black, Point[{foci, -foci}], Purple,
Line[{{foci, #}, {-foci, #}}] & /@ p}, Axes -> True]


Clear[f]
f[x_, y_] := x^2/a^2 + y^2/b^2 == 1
sol = First@
Solve[f @@@ {{0, 3}, {3, 3/2}}, {a, b},
Assumptions -> {a > 0, b > 0}]
x^2/a^2 + y^2/b^2 == 1 /. sol


Thanks to Nasser's tips, the code can be made more concise.

Clear[f]
f[x_, y_] := x^2/a^2 + y^2/b^2 == 1
sol = First@
Solve[f @@@ {{0, 3}, {3, 3/2}}, {a, b},
Assumptions -> {a > 0, b > 0}]
f[x, y] /. sol


• fyi, in your last line x^2/a^2 + y^2/b^2 == 1 /. sol can be replaced by f[x, y] /. sol. No need to copy/paste something you have already written above. Duplication can lead to errors. Commented Jul 10 at 13:05
• @Nasser Written in response to your answers and by reviewing the usage of functions and the @@@ operator from my previous questions. Thank you! Commented Jul 10 at 13:11