if someone help me to understand this I managed to add it to my application?
consconvrule = {
x_ >= y_ -> y - x,
x_ == y_ -> x - y ,
x_ <= y_ -> x - y,
lb_ <= x_ <= ub_ -> (x - lb) (x - ub),
ub_ >= x_ >= lb_ -> (x - lb) (x - ub)
};
KKT[obj_, cons_List, vars_List] :=
Module[
{convcons, gradobj, gradcons, ?},
convcons = (cons /. consconvrule);
{gradobj, gradcons} = D[{obj, convcons}, {vars}];
? = Subscript[?, #] & /@ cons;
LogicalExpand @ Reduce[
Flatten[{
Thread[gradobj == ?.gradcons],
Thread[?*convcons == 0] /.
Subscript[?, Equal[a_, b_]] -> 0,
cons,
{objval == obj}
}],
Join[{objval}, vars, ?],
Reals,
Backsubstitution -> True
]
]
to this application
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,(*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[{szumx, szumy}],
y + RandomReal[{szumx, szumy}]}];
(*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]*)}]]]