Group Abstract Group Abstract

Message Boards Message Boards

0
|
26.9K Views
|
62 Replies
|
30 Total Likes
View groups...
Share
Share this post:

Mathematica creating an ellipse

Posted 11 years ago
Main function to create an ellipse:
a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g

How to add a condition in one function to the three equations:
\[CapitalDelta]=-c d^2 + 2 b d f - a f^2 - b^2 g + a c g
J=-b^2 + a c
I=a+c

conditions:
\[CapitalDelta]!=0
J>0
\[CapitalDelta]/I<0

And if we can do so that once the substitute number three equations? And maybe some random generator to the given numbers satisfy the above conditions.
\[CapitalDelta][a_, b_, c_, d_, f_, g_] := -c d^2 + 2 b d f - a f^2 - b^2 g + a c g
J[a_, b_, c_] := -b^2 + a c
i[a_, c_] := a + c
?[-2, 4, -6, 2, 1, 2]
POSTED BY: Radek Drozd
62 Replies
Posted 11 years ago

how to add KKT conditions to make ellipse and hyperbolas to this:

q = {Off[NMinimize::eit]; Off[NMinimize::lstol];
   DynamicModule[{rng, ptk, szumx, szumy, final}, 
    Panel[Column[{"Theta z przedzia?u od 0 do:", 
       RadioButtonBar[
        Dynamic[rng], {2 Pi -> "2[Pi]", Pi -> "[Pi]", 
         Pi/2 -> "[Pi]/2", Pi/8 -> "[Pi]/8"}], , 
       "Ilo?? generowanych punktów:", 
       RadioButtonBar[
        Dynamic[ptk], {5 -> "5", 20 -> "20", 50 -> "50", 
         100 -> "100"}], , "Warto?ci wektora przesuni?cia", 
       "Warto?? xi:" RadioButtonBar[
         Dynamic[szumx], {-0.1 -> "-0.1", -.5 -> "-0.5", -.9 -> 
           "-0.9", -3 -> "-3"}], 
       "Warto?? yj:" RadioButtonBar[
         Dynamic[szumy], {0.1 -> "0.1", .5 -> "0.5", .9 -> "0.9", 
          3 -> "3"}], 
       Button["Generuj Elipse", 
        While[True,(*losowo wybra? akceptowalne wspó?czynniki*){a, b, 
           c, d, f, g} = RandomReal[{-10, 10}, 6];
         [CapitalDelta] = -c d^2 + 2 b d f - a f^2 - b^2 g + a c g;
         j = -b^2 + a c; i = a + c;
         If[[CapitalDelta] != 0 && j > 0 && [CapitalDelta]/i < 0, 
          Break[]]];
        ellipse = a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g;
        (*?rodek elipsy w ogólnej formie {(c d-b f)/(b^2-a c),(a f-
        b d)/(b^2-a c)}*)points = Table[theta = RandomReal[{0, rng}];

          ksol = FindRoot[(ellipse /. {x -> 
                 k*Cos[theta] + (c d - b f)/(b^2 - a c), 
                y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)}) == 
             0, {k, 1.}];

          Point[{x, y}] /. {x -> 
              k*Cos[theta] + (c d - b f)/(b^2 - a c), 
             y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)} /. 
           ksol, {ptk}];
        nearpoints = 
         points /. 
          Point[{x_, y_}] :> 
           Point[{x + RandomReal[{szumx, szumy}], 
             y + RandomReal[{szumx, szumy}]}];
        (*minimalne i maksymalne warto?ci x i y dla elipsy*)
        yplotrange = 
         Flatten[{y, 
           Sort[{(2*b*d - 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(-b^2 + a*c)), (-2*b*
                 d + 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(b^2 - a*c))}]}];
        xplotrange = 
         Flatten[{x, 
           Sort[{(2*c*d - 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(b^2 - a*c)), (-2*c*
                 d + 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(-b^2 + a*c))}]}];
        (*zminimalizowa? dystans pobliskich punktów dla nowej ogólnej \n    elpisy*)nearCoords = nearpoints[[All, 1]];
        {xs, ys} = Transpose[nearCoords];
        newellipse = aa*x^2 + 2*bb*x*y + cc*y^2 + 2*dd*x + 2*ff*y + gg;
        distance = Plus @@ (newellipse^2 /. {x -> xs, y -> ys});
        {res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
        scaleup = 
         FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
        esolve = Expand[scaleup*(newellipse /. coes)];
        final = 
         Show[ContourPlot[{ellipse == 0, esolve == 0}, 
           Evaluate[xplotrange], Evaluate[yplotrange]], 
          Graphics[points], Graphics[{Red, nearpoints}], 
          ImageSize -> {500, Automatic}];], Dynamic@final
       (*,Dynamic@Grid[Join[{{"points","nearby points"}},
       Transpose[{points,nearpoints}][[All,All,1]]],Frame->All,
       Alignment->Left]*)}]]]};
Grid[{{Labeled["(A)", q], Labeled["(B)", q]}, {Labeled["(C)", q], 
   Labeled["(D)", q]}}]
POSTED BY: Radek Drozd
Posted 11 years ago

really need help with this because trying a couple of days and does not go to me

POSTED BY: Radek Drozd
Posted 11 years ago

how to add a KKT http://community.wolfram.com/groups/-/m/t/193644 to my app with conditions to an ellipse and as a second choice hyperbolas

POSTED BY: Radek Drozd
Posted 11 years ago

what I need to add to show the equation of the ellipse and the original created in order to compare their

q = {Off[NMinimize::eit]; Off[NMinimize::lstol];
   DynamicModule[{rng, ptk, szumx, szumy, final}, 
    Panel[Column[{"Theta z przedzia?u od 0 do:", 
       RadioButtonBar[
        Dynamic[rng], {2 Pi -> "2[Pi]", Pi -> "[Pi]", 
         Pi/2 -> "[Pi]/2", Pi/8 -> "[Pi]/8"}], , 
       "Ilo?? generowanych punktów:", 
       RadioButtonBar[
        Dynamic[ptk], {5 -> "5", 20 -> "20", 50 -> "50", 
         100 -> "100"}], , "Warto?ci wektora przesuni?cia", 
       "Warto?? xi:" RadioButtonBar[
         Dynamic[szumx], {-0.1 -> "-0.1", -.5 -> "-0.5", -.9 -> 
           "-0.9", -3 -> "-3"}], 
       "Warto?? yj:" RadioButtonBar[
         Dynamic[szumy], {0.1 -> "0.1", .5 -> "0.5", .9 -> "0.9", 
          3 -> "3"}], 
       Button["Generuj Elipse", 
        While[True,(*losowo wybra? akceptowalne wspó?czynniki*){a, b, 
           c, d, f, g} = RandomReal[{-10, 10}, 6];
         [CapitalDelta] = -c d^2 + 2 b d f - a f^2 - b^2 g + a c g;
         j = -b^2 + a c; i = a + c;
         If[[CapitalDelta] != 0 && j > 0 && [CapitalDelta]/i < 0, 
          Break[]]];
        ellipse = a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g;
        (*?rodek elipsy w ogólnej formie {(c d-b f)/(b^2-a c),(a f-
        b d)/(b^2-a c)}*)points = Table[theta = RandomReal[{0, rng}];

          ksol = FindRoot[(ellipse /. {x -> 
                 k*Cos[theta] + (c d - b f)/(b^2 - a c), 
                y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)}) == 
             0, {k, 1.}];

          Point[{x, y}] /. {x -> 
              k*Cos[theta] + (c d - b f)/(b^2 - a c), 
             y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)} /. 
           ksol, {ptk}];
        nearpoints = 
         points /. 
          Point[{x_, y_}] :> 
           Point[{x + RandomReal[{szumx, szumy}], 
             y + RandomReal[{szumx, szumy}]}];
        (*minimalne i maksymalne warto?ci x i y dla elipsy*)
        yplotrange = 
         Flatten[{y, 
           Sort[{(2*b*d - 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(-b^2 + a*c)), (-2*b*
                 d + 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(b^2 - a*c))}]}];
        xplotrange = 
         Flatten[{x, 
           Sort[{(2*c*d - 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(b^2 - a*c)), (-2*c*
                 d + 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(-b^2 + a*c))}]}];
        (*zminimalizowa? dystans pobliskich punktów dla nowej ogólnej \n    elpisy*)nearCoords = nearpoints[[All, 1]];
        {xs, ys} = Transpose[nearCoords];
        newellipse = aa*x^2 + 2*bb*x*y + cc*y^2 + 2*dd*x + 2*ff*y + gg;
        distance = Plus @@ (newellipse^2 /. {x -> xs, y -> ys});
        {res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
        scaleup = 
         FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
        esolve = Expand[scaleup*(newellipse /. coes)];
        final = 
         Show[ContourPlot[{ellipse == 0, esolve == 0}, 
           Evaluate[xplotrange], Evaluate[yplotrange]], 
          Graphics[points], Graphics[{Red, nearpoints}], 
          ImageSize -> {500, Automatic}];], Dynamic@final
       (*,Dynamic@Grid[Join[{{"points","nearby points"}},
       Transpose[{points,nearpoints}][[All,All,1]]],Frame->All,
       Alignment->Left]*)}]]]};
Grid[{{Labeled["(A)", q], Labeled["(B)", q]}, {Labeled["(C)", q], 
   Labeled["(D)", q]}}]
POSTED BY: Radek Drozd
Posted 11 years ago

i have this

q = {Off[NMinimize::eit]; Off[NMinimize::lstol];
   DynamicModule[{rng, ptk, final}, 
    Panel[Column[{"Theta z przedzia?u od 0 do:", 
       RadioButtonBar[
        Dynamic[rng], {2 -> "2", Pi/8 -> "\[Pi]/8", Pi/2 -> "\[Pi]/2",
          Pi -> "\[Pi]", 2 Pi -> "2\[Pi]"}], 
       "Ilo?? generowanych punktów:", 
       RadioButtonBar[
        Dynamic[ptk], {5 -> "5", 20 -> "20", 50 -> "50", 
         100 -> "100"}], 
       Button["Generuj Elipse", 
        While[True,(*Randomly choose coefficients until \
acceptable*){a, b, c, d, f, g} = RandomReal[{-10, 10}, 6];
         \[CapitalDelta] = -c d^2 + 2 b d f - a f^2 - b^2 g + a c g;
         j = -b^2 + a c; i = a + c;
         If[\[CapitalDelta] != 0 && j > 0 && \[CapitalDelta]/i < 0, 
          Break[]]];
        ellipse = a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g;
        (*Center of an ellipse in general form is{(c d-b f)/(b^2-
        a c),(a f-b d)/(b^2-a c)}*)
        points = Table[theta = RandomReal[{0, rng}];

          ksol = FindRoot[(ellipse /. {x -> 

                     k*Cos[theta] + (c d - b f)/(b^2 - a c), 
                y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)}) == 
             0, {k, 1.}];

          Point[{x, y}] /. {x -> 
              k*Cos[theta] + (c d - b f)/(b^2 - a c), 
             y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)} /. 
           ksol, {ptk}];
        nearpoints = 
         points /. 
          Point[{x_, y_}] :> 
           Point[{x + RandomReal[{-.1, .1}], 
             y + RandomReal[{-.1, .1}]}];
        (*ellipse x and y min and max values*)
        yplotrange = 
         Flatten[{y, 
           Sort[{(2*b*d - 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(-b^2 + a*c)), (-2*b*
                 d + 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(b^2 - a*c))}]}];
        xplotrange = 
         Flatten[{x, 
           Sort[{(2*c*d - 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(b^2 - a*c)), (-2*c*
                 d + 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(-b^2 + a*c))}]}];
        (*minimize distance of near points to a new general ellipse*)
        nearCoords = nearpoints[[All, 1]];
        {xs, ys} = Transpose[nearCoords];
        newellipse = aa*x^2 + 2*bb*x*y + cc*y^2 + 2*dd*x + 2*ff*y + gg;
        distance = Plus @@ (newellipse^2 /. {x -> xs, y -> ys});
        {res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
        scaleup = 
         FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
        esolve = Expand[scaleup*(newellipse /. coes)];
        final = 
         Show[ContourPlot[{ellipse == 0, esolve == 0}, 
           Evaluate[xplotrange], Evaluate[yplotrange], 
           ImageSize -> {200, Automatic}], Graphics[points], 
          Graphics[{Red, nearpoints}]];], Dynamic@final}]]]};
Grid[{{Labeled["(A)", q], Labeled["(B)", q]}, {Labeled["(C)", q], 
   Labeled["(D)", q]}}]

and i want add this to chose between n and m

m = {{res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
   scaleup = FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
   esolve = Expand[scaleup*(newellipse /. coes)];};

n = {{res, coes} = 
    FindMinimum[{distance, -bb^2 + aa*cc > 
       0., (-cc*dd^2 + 2 bb*dd*ff - aa*ff^2 - bb^2*gg + 
          aa*cc*gg)/(aa + cc) < 0.}, {{aa, a}, {bb, b}, {cc, c}, {dd, 
       d}, {ff, f}, {gg, g}}];
   esolve = newellipse /. coes;};

it's possible to add some menu to make it all work?

POSTED BY: Radek Drozd
Posted 11 years ago

I would like to show two visualization applications and mark it as

 q = {application code}; Show [q, q] 

but i have some errors, how to delete that errors?

Image

POSTED BY: Radek Drozd
Posted 11 years ago

how to implement optimizations but always came to the ellipse (ie, must be met inequality and optimize the assumption of the Kuhn Tucker). in some menu to choose with or without optimizations

Off[NMinimize::eit]; Off[NMinimize::lstol];
DynamicModule[{rng, ptk, final}, 
 Panel[Column[{"Theta z przedzia?u od 0 do:", 
    RadioButtonBar[
     Dynamic[rng], {2 -> "2", Pi/8 -> "\[Pi]/8", Pi/2 -> "\[Pi]/2", 
      Pi -> "\[Pi]", 2 Pi -> "2\[Pi]"}], 
    "Ilo?? generowanych punktów:", 
    RadioButtonBar[
     Dynamic[ptk], {5 -> "5", 20 -> "20", 50 -> "50", 100 -> "100"}], 
    Button["Generuj Elipse", 
     While[True,(*Randomly choose coefficients until acceptable*){a, 
        b, c, d, f, g} = RandomReal[{-10, 10}, 6];
      \[CapitalDelta] = -c d^2 + 2 b d f - a f^2 - b^2 g + a c g;
      j = -b^2 + a c; i = a + c;
      If[\[CapitalDelta] != 0 && j > 0 && \[CapitalDelta]/i < 0, 
       Break[]]];
     ellipse = a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g;
     (*Center of an ellipse in general form is{(c d-b f)/(b^2-
     a c),(a f-b d)/(b^2-a c)}*)
     points = Table[theta = RandomReal[{0, rng}];
       ksol = 
        FindRoot[(ellipse /. {x -> 
              k*Cos[theta] + (c d - b f)/(b^2 - a c), 
             y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)}) == 0, {k, 
          1.}];
       Point[{x, y}] /. {x -> k*Cos[theta] + (c d - b f)/(b^2 - a c), 
          y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)} /. ksol, {ptk}];
     nearpoints = 
      points /. 
       Point[{x_, y_}] :> 
        Point[{x + RandomReal[{-.1, .1}], y + RandomReal[{-.1, .1}]}];
     (*ellipse x and y min and max values*)
     yplotrange = 
      Flatten[{y, 
        Sort[{(2*b*d - 2*a*f + 
             Sqrt[(2*b*d - 2*a*f)^2 - 
               4*(b^2 - a*c)*(d^2 - a*g)])/(2*(-b^2 + a*c)), (-2*b*
              d + 2*a*f + 
             Sqrt[(2*b*d - 2*a*f)^2 - 
               4*(b^2 - a*c)*(d^2 - a*g)])/(2*(b^2 - a*c))}]}];
     xplotrange = 
      Flatten[{x, 
        Sort[{(2*c*d - 2*b*f + 
             Sqrt[(-2*c*d + 2*b*f)^2 - 
               4*(b^2 - a*c)*(f^2 - c*g)])/(2*(b^2 - a*c)), (-2*c*d + 
             2*b*f + 
             Sqrt[(-2*c*d + 2*b*f)^2 - 
               4*(b^2 - a*c)*(f^2 - c*g)])/(2*(-b^2 + a*c))}]}];
     (*minimize distance of near points to a new general ellipse*)
     nearCoords = nearpoints[[All, 1]];
     {xs, ys} = Transpose[nearCoords];
     newellipse = aa*x^2 + 2*bb*x*y + cc*y^2 + 2*dd*x + 2*ff*y + gg;
     distance = Plus @@ (newellipse^2 /. {x -> xs, y -> ys});
     {res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
     scaleup = FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
     esolve = Expand[scaleup*(newellipse /. coes)];
     final = 
      Show[ContourPlot[{ellipse == 0, esolve == 0}, 
        Evaluate[xplotrange], Evaluate[yplotrange]], Graphics[points],
        Graphics[{Red, nearpoints}], ImageSize -> {500, Automatic}];],
     Dynamic@final
    (*,Dynamic@Grid[Join[{{"points","nearby points"}},
    Transpose[{points,nearpoints}][[All,All,1]]],Frame->All,
    Alignment->Left]*)}]]]
POSTED BY: Radek Drozd
Posted 11 years ago

how to show for which coefficients come ellipse parabola or hyperbola, etc. how to do that?

POSTED BY: Radek Drozd
Posted 11 years ago

wow that nice thank you very much, and whether it is possible to plot was shown more in the middle to those lines which goes out outside of the frame were more visible? or allow scaling with some slider or something else

and I have a show for which coefficients come ellipse parabola or hyperbola, etc.

POSTED BY: Radek Drozd

Sure. FindMinimum using the starting ellipse coefficients as a search starting point with no other constraints :

{res, coes} = FindMinimum[distance, {{aa, a}, {bb, b}, {cc, c}, {dd, d}, {ff, f}, {gg, g}}];

FindMinimum using the starting ellipse coefficients and the initial constraints :

{res, coes} = FindMinimum[{distance,
  -bb^2 + aa*cc > 0.,
  (-cc*dd^2 + 2 bb*dd*ff - aa*ff^2 - bb^2*gg + aa*cc *gg)/(aa + cc) < 0.}, 
{{aa, a}, {bb, b}, {cc, c}, {dd, d}, {ff, f}, {gg, g}}];

FindMinimum only finds a local minimum, where NMinimize can find unconstrained global minima :

{res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];

There are lots of ways to add a selection, this uses RadioButtonBar :

RadioButtonBar[Dynamic[rng], {Pi/2 -> "\[Pi]/2", 2 Pi -> "2\[Pi]"}]

The results should look like this :

enter image description here

The code now looks like this :

Off[NMinimize::eit]; Off[NMinimize::lstol];
DynamicModule[{rng, final},
 Panel[Column[{
RadioButtonBar[
 Dynamic[rng], {Pi/2 -> "\[Pi]/2", 2 Pi -> "2\[Pi]"}],
Button["Generuj Elipse",
 While[True,(*Randomly choose coefficients until acceptable*)
  {a, b, c, d, f, g} = RandomReal[{-10, 10}, 6];
  \[CapitalDelta] = -c d^2 + 2 b d f - a f^2 - b^2 g + a c g;
  j = -b^2 + a c; i = a + c;
  If[\[CapitalDelta] != 0 && j > 0 && \[CapitalDelta]/i < 0, 
   Break[]]];
 ellipse = a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g;

 (*Center of an ellipse in general form is{(c d-b f)/(b^2-a c),(a f-b d)/(b^2-a c)}*)
 points = Table[theta = RandomReal[{0, rng}];
   ksol = 
    FindRoot[(ellipse /. {x -> 
          k*Cos[theta] + (c d - b f)/(b^2 - a c), 
         y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)}) == 0, {k, 
      1.}];
   Point[{x, y}] /. {x -> k*Cos[theta] + (c d - b f)/(b^2 - a c), 
      y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)} /. ksol, {100}];
 nearpoints = 
  points /. 
   Point[{x_, y_}] :> 
    Point[{x + RandomReal[{-.1, .1}], y + RandomReal[{-.1, .1}]}];
 (*ellipse x and y min and max values*)

 yplotrange = 
  Flatten[{y, Sort[{(2*b*d - 2*a*f + Sqrt[(2*b*d - 2*a*f)^2 - 4*(b^2 - a*c)*(d^2 - a*g)])/(2*(-b^2 + a*c)), 
             (-2*b*d + 2*a*f + Sqrt[(2*b*d - 2*a*f)^2 - 4*(b^2 - a*c)*(d^2 - a*g)])/(2*(b^2 - a*c))}]}];
 xplotrange = 
  Flatten[{x, Sort[{(2*c*d - 2*b*f + Sqrt[(-2*c*d + 2*b*f)^2 - 4*(b^2 - a*c)*(f^2 - c*g)])/(2*(b^2 - a*c)), 
             (-2*c*d + 2*b*f + Sqrt[(-2*c*d + 2*b*f)^2 - 4*(b^2 - a*c)*(f^2 - c*g)])/(2*(-b^2 + a*c))}]}];
 (*minimize distance of near points to a new general ellipse*)

 nearCoords = nearpoints[[All, 1]];
 {xs, ys} = Transpose[nearCoords];
 newellipse = aa*x^2 + 2*bb*x*y + cc*y^2 + 2*dd*x + 2*ff*y + gg;
 distance = Plus @@ (newellipse^2 /. {x -> xs, y -> ys});
 {res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
 scaleup = FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
 esolve = Expand[scaleup*(newellipse /. coes)];

 final = Show[
   ContourPlot[{ellipse == 0, esolve == 0}, Evaluate[xplotrange], 
    Evaluate[yplotrange]],
   Graphics[points], Graphics[{Red, nearpoints}],
   ImageSize -> {500, Automatic}];],

Dynamic@final
(*,Dynamic@Grid[Join[{{"points","nearby points"}},
Transpose[{points,nearpoints}][[All,All,1]]],
Frame\[Rule]All,Alignment\[Rule]Left]*)}]]
]
Posted 11 years ago
Be respectful. Review our Community Guidelines to understand your role and responsibilities. Community Terms of Use