# Numerical Minimization of Large Expression

I want to minimize a function which contains ArcTan functions. The function is also very complicated.

I have the following code.

LenFact = 1.3745/0.31571;
xC = 0.157392 LenFact; yC = 0.035495 LenFact;

q={x1[t],y1[t],x2[t],y2[t],x3[t],y3[t],x4[t],y4[t],x5[t],y5[t],x6[t],y6[t]};

CE = {
{-0.609155 + x1[t]^2 + y1[t]^2},
{-0.0383062 + (-x1[t] + x2[t])^2 + (-y1[t] + y2[t])^2},
{-1.64323 + (-0.685234 + x2[t])^2 + (-0.154534 + y2[t])^2},
{-1.88925 + (-x1[t] + x6[t])^2 + (-y1[t] + y6[t])^2},
{-0.428366 + ArcTan[-x1[t] + x2[t], -y1[t] + y2[t]] -
ArcTan[-x1[t] + x6[t], -y1[t] + y6[t]]},
{-0.783885 + (-x3[t] + x4[t])^2 + (-y3[t] + y4[t])^2},
{-0.0353049 + (-x4[t] + x5[t])^2 + (-y4[t] + y5[t])^2},
{-0.25649 + ArcTan[-x3[t] + x4[t], -y3[t] + y4[t]] -
ArcTan[-x4[t] + x5[t], -y4[t] + y5[t]]},
{-0.0228637 + (-x5[t] + x6[t])^2 + (-y5[t] + y6[t])^2},
{-0.0138516 + (-x2[t] + x3[t])^2 + (-y2[t] + y3[t])^2},
{-ArcTan[0.685234 - x2[t], 0.154534 - y2[t]] +
ArcTan[-x2[t] + x3[t], -y2[t] + y3[t]]}
};

y6Start = 0.15 LenFact;

VPenalty={{3.90351 + 1.13817*10^6 (2.74102 - 1. ArcTan[x1[t], y1[t]])^2 +
3006.52 (2.92648 -
1. ArcTan[-0.685234 + x3[t], -0.154534 + y3[t]])^2 +
1082.47 (2.78921 -
1. ArcTan[-0.685234 + x3[t], -0.154534 + y3[t]] +
ArcTan[-1. x3[t] + x4[t], -1. y3[t] + y4[t]])^2 +
6825.76 (2.48452 - 1. ArcTan[x1[t], y1[t]] +
ArcTan[-1. x1[t] + x6[t], -1. y1[t] + y6[t]])^2 +
239.418 y1[t] + 29.1166 y2[t] + 6.00899 y3[t] + 7.28423 y4[t] +
4.93484 y5[t] + 97.0584 y6[t] +
1.90484*10^8 ((-ArcTan[0.685234 - x2[t], 0.154534 - y2[t]] +
ArcTan[-x2[t] + x3[t], -y2[t] + y3[t]])^2 + (-0.25649 +
ArcTan[-x3[t] + x4[t], -y3[t] + y4[t]] -
ArcTan[-x4[t] + x5[t], -y4[t] + y5[t]])^2 + (-0.428366 +
ArcTan[-x1[t] + x2[t], -y1[t] + y2[t]] -
ArcTan[-x1[t] + x6[t], -y1[t] + y6[t]])^2 + (-0.609155 +
x1[t]^2 +
y1[t]^2)^2 + (-1.64323 + (-0.685234 + x2[t])^2 + (-0.154534 +
y2[t])^2)^2 + (-0.0383062 + (-x1[t] + x2[t])^2 + (-y1[t] +
y2[t])^2)^2 + (-0.0138516 + (-x2[t] + x3[t])^2 + (-y2[t] +
y3[t])^2)^2 + (-0.783885 + (-x3[t] + x4[t])^2 + (-y3[t] +
y4[t])^2)^2 + (-0.0353049 + (-x4[t] + x5[t])^2 + (-y4[t] +
y5[t])^2)^2 + (-1.88925 + (-x1[t] + x6[t])^2 + (-y1[t] +
y6[t])^2)^2 + (-0.0228637 + (-x5[t] + x6[t])^2 + (-y5[t] +
y6[t])^2)^2)}};

\[Theta]1 = ArcTan[x1[t], y1[t]];
\[Theta]21 = ArcTan[x2[t] - x1[t], y2[t] - y1[t]];
\[Theta]3 = ArcTan[x3[t] - xC, y3[t] - yC];
\[Theta]22 = ArcTan[x6[t] - x1[t], y6[t] - y1[t]];
\[Theta]41 = ArcTan[x4[t] - x3[t], y4[t] - y3[t]];
\[Theta]42 = ArcTan[x5[t] - x4[t], y5[t] - y4[t]];
\[Theta]5 = ArcTan[x6[t] - x5[t], y6[t] - y5[t]];

eqbr = NMinimize[{VPenalty[[1, 1]], Flatten[CE] == 0,
90 \[Degree] < \[Theta]1 < 180 \[Degree],
20 \[Degree] < \[Theta]21 < 95 \[Degree],
145 \[Degree] < \[Theta]3 < 180 \[Degree],
80 \[Degree] < \[Theta]5 < 100 \[Degree], 0 < y6[t] < y6Start},
q, Reals, MaxIterations -> 10^4]


It is running for several hours with no result. Can any body suggest any specific way of minimizing such large scale expression? I also could not give initial points. It is always showing syntax error. Is it not possible to give one initial point only for each variable?

• I am sorry. I have incorporated CE in the code now. Commented Mar 16, 2019 at 15:43
• And what about y6Start? Commented Mar 16, 2019 at 15:45
• And after all: What is the context? This looks a bit like discrete bending energy minimization with some angle constraints... Maybe the problem can be rephrased into another one that is simpler to solve. Commented Mar 16, 2019 at 15:47
• y6Start = 0.15 LenFact; This is about finding the static equilibrium of a mechanism. Static equilibrium states can be found by minimizing the potential energy function. Commented Mar 16, 2019 at 15:50

## 2 Answers

I corrected typos. The code is fast and gives an answer, albeit with messages.

LenFact = 1.3745/0.31571;
xC = 0.157392 LenFact; yC = 0.035495 LenFact;

q = {x1[t], y1[t], x2[t], y2[t], x3[t], y3[t], x4[t], y4[t], x5[t],
y5[t], x6[t],
y6[t]}; CE = {{-0.609155 + x1[t]^2 +
y1[t]^2}, {-0.0383062 + (-x1[t] + x2[t])^2 + (-y1[t] +
y2[t])^2}, {-1.64323 + (-0.685234 + x2[t])^2 + (-0.154534 +
y2[t])^2}, {-1.88925 + (-x1[t] + x6[t])^2 + (-y1[t] +
y6[t])^2}, {-0.428366 +
ArcTan[-x1[t] + x2[t], -y1[t] + y2[t]] -
ArcTan[-x1[t] + x6[t], -y1[t] +
y6[t]]}, {-0.783885 + (-x3[t] + x4[t])^2 + (-y3[t] +
y4[t])^2}, {-0.0353049 + (-x4[t] + x5[t])^2 + (-y4[t] +
y5[t])^2}, {-0.25649 + ArcTan[-x3[t] + x4[t], -y3[t] + y4[t]] -
ArcTan[-x4[t] + x5[t], -y4[t] +
y5[t]]}, {-0.0228637 + (-x5[t] + x6[t])^2 + (-y5[t] +
y6[t])^2}, {-0.0138516 + (-x2[t] + x3[t])^2 + (-y2[t] +
y3[t])^2}, {-ArcTan[0.685234 - x2[t], 0.154534 - y2[t]] +
ArcTan[-x2[t] + x3[t], -y2[t] + y3[t]]}};

VPenalty = {{3.90351 +
1.13817*10^6 (2.74102 - 1. ArcTan[x1[t], y1[t]])^2 +
3006.52 (2.92648 -
1. ArcTan[-0.685234 + x3[t], -0.154534 + y3[t]])^2 +
1082.47 (2.78921 -
1. ArcTan[-0.685234 + x3[t], -0.154534 + y3[t]] +
ArcTan[-1. x3[t] + x4[t], -1. y3[t] + y4[t]])^2 +
6825.76 (2.48452 - 1. ArcTan[x1[t], y1[t]] +
ArcTan[-1. x1[t] + x6[t], -1. y1[t] + y6[t]])^2 +
239.418 y1[t] + 29.1166 y2[t] + 6.00899 y3[t] + 7.28423 y4[t] +
4.93484 y5[t] + 97.0584 y6[t] +
1.90484*10^8 ((-ArcTan[0.685234 - x2[t], 0.154534 - y2[t]] +
ArcTan[-x2[t] + x3[t], -y2[t] + y3[t]])^2 + (-0.25649 +
ArcTan[-x3[t] + x4[t], -y3[t] + y4[t]] -
ArcTan[-x4[t] + x5[t], -y4[t] + y5[t]])^2 + (-0.428366 +
ArcTan[-x1[t] + x2[t], -y1[t] + y2[t]] -
ArcTan[-x1[t] + x6[t], -y1[t] + y6[t]])^2 + (-0.609155 +
x1[t]^2 +
y1[t]^2)^2 + (-1.64323 + (-0.685234 +
x2[t])^2 + (-0.154534 +
y2[t])^2)^2 + (-0.0383062 + (-x1[t] +
x2[t])^2 + (-y1[t] +
y2[t])^2)^2 + (-0.0138516 + (-x2[t] +
x3[t])^2 + (-y2[t] +
y3[t])^2)^2 + (-0.783885 + (-x3[t] +
x4[t])^2 + (-y3[t] +
y4[t])^2)^2 + (-0.0353049 + (-x4[t] +
x5[t])^2 + (-y4[t] + y5[t])^2)^2 + (-1.88925 + (-x1[t] +
x6[t])^2 + (-y1[t] +
y6[t])^2)^2 + (-0.0228637 + (-x5[t] +
x6[t])^2 + (-y5[t] + y6[t])^2)^2)}};

\[Theta]1 = ArcTan[x1[t], y1[t]];
\[Theta]21 = ArcTan[x2[t] - x1[t], y2[t] - y1[t]];
\[Theta]3 = ArcTan[x3[t] - xC, y3[t] - yC];
\[Theta]22 = ArcTan[x6[t] - x1[t], y6[t] - y1[t]];
\[Theta]41 = ArcTan[x4[t] - x3[t], y4[t] - y3[t]];
\[Theta]42 = ArcTan[x5[t] - x4[t], y5[t] - y4[t]];
\[Theta]5 = ArcTan[x6[t] - x5[t], y6[t] - y5[t]];

With[{y6Start = 0.15 LenFact},
eqbr = NMinimize[{VPenalty[[1, 1]],
Flatten[CE] == Table[0, {Length[CE]}], Pi/2 < \[Theta]1 < Pi,
20*Pi/180 < \[Theta]21 < 95*Pi/180, 145*Pi/180 < \[Theta]3 < Pi,
80*Pi/180 < \[Theta]5 < 100*Pi/180, 0 < y6[t] < y6Start}, q,
Reals]]
(*{1652.07, {x1[t] -> -0.729157, y1[t] -> 0.278358, x2[t] -> -0.573507,
y2[t] -> 0.397027, x3[t] -> -0.457944, y3[t] -> 0.374765,
x4[t] -> 0.423225, y4[t] -> 0.460944, x5[t] -> 0.60875,
y5[t] -> 0.431194, x6[t] -> 0.611298, y6[t] -> 0.582379}}*)

• Thanks Alex. At least the constraints are matching to a reasonable degree. Although I am still not sure about the obtained value of Vtotal to be the global one. Commented Mar 16, 2019 at 16:30
• Here VPenalty[] is a non-linear function, so the min can be local. Commented Mar 16, 2019 at 17:10

The method "RandomSearch" usually gives a result reasonably quickly (other than using FindMinimum). It's less robust, though, than the other methods. The following takes about 30 sec.

eqbr = NMinimize[{VPenalty[[1, 1]], Flatten[CE] == 0,
90 ° < θ1 < 180 °, 20 ° < θ21 < 95 °,
145 ° < θ3 < 180 °, 80 ° < θ5 < 100 °, 0 < y6[t] < y6Start}, q,
Method -> "RandomSearch"]


NMinimize::incst: NMinimize was unable to generate any initial points satisfying the inequality constraints...

(*
{161.292, {x1[t] -> -0.718754, y1[t] -> 0.30422, x2[t] -> -0.567146,
y2[t] -> 0.427983, x3[t] -> -0.452162, y3[t] -> 0.402877,
x4[t] -> 0.424915, y4[t] -> 0.523793, x5[t] -> 0.611471,
y5[t] -> 0.501395, x6[t] -> 0.610863, y6[t] -> 0.652602}}
*)


While NMinimize did not find good starting points, it found a solution within the constraints:

{90 ° < θ1 < 180 °, 20 ° < θ21 < 95 °, 145 ° < θ3 < 180 °,
80 ° < θ5 < 100 °, 0 < y6[t] < y6Start} /.
Last@eqbr
(*  {True, True, True, True, True}  *)