# How can I transform a quadratic into an factored equation? [duplicate]

How can I transform a quadratic polynomial in x and y into an equation of the form $(x-a)^2+(y-b)^2=c$?

f = -9 x + (3 x^2)/4 + (3 y^2)/4;


Completing the Square

CompleteSquare[f_, x_] :=
Module[{a, b, c},
{c, b, a} = CoefficientList[f, x];
a (x + b/2/a)^2 + Simplify[(c - b^2/4/a)]]

CompleteSquare[f, x]

(*
3/4 (-6 + x)^2 + 3/4 (-36 + y^2)
*)


What I want is the following:

(x - 6)^2 + y^2 == 36


coeffs[expr_, vars_]:= Flatten[CoefficientList[expr, vars]]

CompleteSquare[expr_, {x_, y_}]:= Module[{a, b, c, c1, c2, eqns, sols},
eqns = Thread[coeffs[c1 (x - a)^2 + c2 (y - b)^2 - c, {x, y}] - coeffs[f, {x, y}] == 0];
Check[sols = Solve[eqns, {a, b, c, c1, c2}], Return@\$Failed];
(x - a)^2 + c2/c1 (y - b)^2 == c/c1 /. sols
]


Now for the specific example.

f = -9 x + (3 x^2)/4 + (3 y^2)/4;
CompleteSquare[f, {x, y}]

(*{(-6 + x)^2 + y^2 == 36}*)


Let first make the CompleteSquare[f_, x_] function according to your reference.

(* Destination Function = X1(x) = A*(x-B)^2, X2 = C  such that X1+X2 = f *)

CompleteSquare[f_, x_] := Module[{a, b, c},
{c, b, a} = Table[Coefficient[f, x, i], {i, 0, 2}]; X1 = a (x + b/(2 a))^2;
X2 = (c - b^2/(4 a)); Print[X1, "+" X2]]


When you run CompleteSquare[f, x] it will create X1 and X2 which will have the perfect square and additive part and Print their sum. For your example

f = -9 x + (3 x^2)/4 + (3 y^2)/4;

CompleteSquare[f, x]
X1
X2


And your desired equation is X1 + X2 == 0.

• You really shouldn't define functions in a way that they modify external variables!! Jul 12, 2013 at 14:46