Message Boards Message Boards

GROUPS:

Solving equation of a circle in Mathematica

Posted 4 months ago
1463 Views
|
14 Replies
|
3 Total Likes
|

How would you go about solving the function: x^2+y^2- 2x + 6y + 9 = 0 in Mathematica? This function has the solution of: (x-1)^2+(y+ 3)^2 = 1, which is a circle of radius 1 centered at (1,-3).

When using the Solve[] function of:

Solve[x^2 + y^2 - 2 x + 6 y + 9 == 0 {x, y}]
 I get {{y -> -3 - Sqrt[2 x - x^2]}, {y -> -3 + Sqrt[2 x - x^2]}}

which is far from my desired answer.

I know I am omitting something from the function, but cannot figure out what.

Thanks,

Mitch Sandlin

14 Replies

Probably you what:

c = ImplicitRegion[x^2 + y^2 - 2 x + 6 y + 9 < 0, {x, y}];
center = RegionCentroid[c]
radius = Sqrt[Area[c]/Pi]

(*{1, -3}*)
(* 1 *)

For more solution for this problem see here.

Regards M.I.

Posted 4 months ago
template = (x - a)^2 + (y - b)^2 + c

expression = x^2 + y^2 - 2 x + 6 y + 9

template /. First@Solve[ForAll[{x, y}, expression == template]]
(* -1+(-1+x)^2+(3+y)^2 *)

I picked this up from a MSE discussion. See the answer from minthao_2011, towards the end.

Hi Hans;

That is a neat example. However when I tried it on my system, I did not get the same results - see attached.

Attachments:

Hi Hans;

Please ignore my prior response because it was me at fault - I wasn't clearing the variables correctly. Your neat example works fine and thanks again.

Posted 4 months ago

Mitchell,

As you have probably already noted this method can also be used in general to "complete the square"

template = (x - a)^2 + b

expression = x^2 + 10 x - 39

z = Solve[ForAll[x, expression == template]]

template /. First@z // TraditionalForm
(x + 5)^2 - 64

Thanks Hans, and I did notice that the technique could be used for "Completing the Square", which I find very interested.

I did come up with one additional question concerning handling coefficient's to x^2 and y^2 - see attached. I am not quite sure how to handle these additional coefficients in the technique, which is causing Mathematica to generate computational errors. Completing the Square manually, I get: (x + 3/2)^2 + (y - 3)^2 =1

Thanks,

Mitch Sandlin

Attachments:
Posted 4 months ago

Hi

There is a catch: The method requires coefficients of the $x^2$ and $y^2$ terms to be +1.

So in your case divide the expression by 4. This will give the expected output.

template = (x-a)^2 + (y-b)^2 + c

expression = 4 x^2 + 4 y^2 + 12 x - 24 y + 41

template /. First@Solve[ForAll[{x, y}, expression/4 == template]]
(* -1 + (3/2+x)^2 + (-3+y)^2 *)

One of the many useful functions, CompleteTheSquare, from David Parks's Presentations application, makes this almost immediate:

    poly = Fold[CompleteTheSquare, x^2 + y^2 - 2 x + 6 y + 9, {x, y}]
(* -1 + (-1 + x)^2 + (3 + y)^2 *)

And then use the built-in AddSides:

    AddSides[poly == 0, -First@poly]
(* (-1 + x)^2 + (3 + y)^2 == 1 *)

Hi Murray;

I tried duplicating your example, but did not get the same results. Please see attached notebook and give me your thoughts.

Thanks so much,

Mitch Sandlin

Attachments:

You did not load Parks's Presentations package, which you must first obtain, install, and then load:

<<Presentations`

You could as well do it directly using elementary Mma-commands

gl = x^2 + y^2 - 2 x + 6 y + 9;

pp = {#[[2, 1]], #[[1, 2]], #[[1, 1]]} &[CoefficientList[gl, {x, y}]];

res = Factor[gl - pp[[3]] + pp[[1]]^2/4 /. y -> 0] +
  Factor[gl - pp[[3]] + pp[[2]]^2/4 /. x -> 0] -
  pp[[1]]^2/4 - pp[[2]]^2/4 + pp[[3]]

Or somewhat more general

gl = a x^2 + c y^2 + b x + d y + r

pp = CoefficientList[gl, {x, y}];
c2 = pp[[3, 1]];
c1 = pp[[2, 1]];
u2 = pp[[1, 3]];
u1 = pp[[1, 2]];
gl1 = gl - pp[[1, 1]];
t1 = Factor[(gl1 /. y -> 0)/c2 + (c1/2/c2)^2];
t2 = Factor[(gl1 /. x -> 0)/u2 + (u1/2/u2)^2];

tgl = c2 t1 + u2 t2  - c1^2/c2/4 - u1^2/u2/4 + pp[[1, 1]]

gl - tgl // FullSimplify
Posted 4 months ago

Another take. This is a function that takes a polynomial in any number of variables. Where all variables appear to the 2nd power, and not higher. The 2nd power terms has to have coefficients of +1:

completeTheSquare[poly_] := With[
  {form = Total@Table[(i + Unique[])^2, {i, Variables@poly}] + Unique[]},
  form /. Flatten@Solve[ForAll[Evaluate@Variables@poly, poly == form]]
] /;
And[
  Union[Exponent[poly, Variables@poly]] == {2},
  Union[Coefficient[poly, #, 2] & /@ Variables@poly] == {1}
]

Applying the function:

completeTheSquare[t^2 + 10 t - 39]
(* -64+(5+t)^2 *)

completeTheSquare[x^2 + y^2 - 2 x + 6 y + 9]
(* -1+(-1+x)^2+(3+y)^2 *)

completeTheSquare[i^2 + j^2 + k^2 - 2 i - 4 j - 6 k - 11]
(* -25+(-1+i)^2+(-2+j)^2+(-3+k)^2 *)

Use the function CompleteTheSquare from the Wolfram Function Repository: https://resources.wolframcloud.com/FunctionRepository/resources/CompleteTheSquare

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract