# FindMinimum diverges

I just enocountered following issue, FindMinimjum explicitly diverges — final result is WORSE than initial supplied conditions.

During evaluation of In:= FindMinimum::cvmit: Failed to converge to the requested accuracy or precision within 500 iterations.

{{21552.2, {k1 -> 7.60098, k2 -> -13.9876, k3 -> 8.31783, k4 -> 16.4603, k5 -> 11.6401, k6 -> 6.50849}}}

Whereas value at arbitrary initial conditions is:

Re[finaleq3] /.
{d -> 0.5, d1 -> 0.55, k1 -> 1, k2 -> 1, k3 -> 1, k4 -> 1, k5 -> 1, k6 -> 1}


2590.2

Here's a minimum working part of my script:

dmat[d_] := {{1, -2 d, d*d}, {0, 1, -d}, {0, 0, 1}};
c0[k_, l_] := Cos[Sqrt[k]*l];
s0[k_, l_] := 1/Sqrt[k]*Sin[Sqrt[k]*l];
cp[k_, l_] := -Sqrt[k]*Sin[Sqrt[k]*l];
sp[k_, l_] := Cos[Sqrt[k]*l];
frmat[k_,l_] :=
{{c0[k, l]*c0[k, l], -2*s0[k, l]*c0[k, l], s0[k, l]*s0[k, l]},
{-c0[k, l]*cp[k, l], s0[k, l]*cp[k, l] + sp[k, l]*c0[k, l], -s0[k, l]*sp[k, l]},
{cp[k, l]*cp[k, l], -2*cp[k, l]*sp[k, l], sp[k, l]*sp[k, l]}}
finalmatx =
dmat[d1] . frmat[k6, 0.1] . dmat[d] . frmat[k5, 0.1] . dmat[d] . frmat[k4, 0.1] .
dmat[d] . frmat[k3, 0.1] . dmat[d] . frmat[k2, 0.1] . dmat[d] . frmat[k1, 0.1] .
dmat[d1];
finalmaty =
dmat[d1] . frmat[-k6, 0.1] . dmat[d] . frmat[-k5, 0.1] . dmat[d] .
frmat[-k4, 0.1] . dmat[d] . frmat[-k3, 0.1] . dmat[d] . frmat[-k2, 0.1] .
dmat[d] . frmat[-k1, 0.1] . dmat[d1];

tx03 = {31.896, -11.249, 3.999};
txu3 = {3.915, -1.868, 1.147};
ty03 = {20.115, 3.350, 0.608};
tyu3 = {1.193, 0.607, 1.147};
finaleqx3 = (txu3 - finalmatx.tx03).(txu3 - finalmatx.tx03);
finaleqy3 = (tyu3 - finalmaty.ty03).(tyu3 - finalmaty.ty03);
finaleq3 = finaleqx3 + finaleqy3;
resultlist3 = {};

AppendTo[
resultlist3,
FindMinimum[
{Re[finaleq3] /. {d -> 0.5, d1 -> 0.55},
-20 <= k1 <= 20, -20 <= k2 <= 20, -20 <= k3 <= 20, -20 <= k4 <= 20,
-20 <= k5 <= 20, -20 <= k6 <= 20},
{{k1, 1}, {k2, 1}, {k3, 1}, {k4, 1}, {k5, 1}, {k6, 1}}]]


### Note 1

Matrices frmat are purely linear and equations finaleqx3, finaleqy3 and finaleq3 are real, but for some reason Mathematica insists on keeping a vanished imaginary term, which is the reason why I had to use Re.

finaleq3 /.
{d -> 0.5, d1 -> 0.55, k1 -> 1, k2 -> 1, k3 -> 1, k4 -> 1, k5 -> 1, k6 -> 1}


590.2 + 0. I

It's easy to check that all imaginary numbers cancel out within c0, s0, cp and sp terms used in frmat and have no right appearing dragging on to final result.

### Note 2

I'm making such minimizations for different values of tx0 and ty0 (script here is for 3rd set of tx0/ty0 values), some minimize successfully, some diverge like the case discussed above.

I'd like to find out what's happening and how can I work around this problem. Thanks for any help.

### Edit

I just found out that increasing MaxIterations to 5000 allowed this particular minimization to evaluate successfully. However, MaxIterations did not help with a different set of tx0/ty0. Besides that, I'm quite perplexed as to why intermediate values in successful optimisation are so much worse than initial ones.

• The Complex numbers are introduced by taking Sqrt of a negative number. As with most computer languages, computations with type Complex do not automatically revert to type Real when the imaginary part happens to vanish. Nov 29 '19 at 18:10

obj = finaleq3 /. {d -> 0.5, d1 -> 0.55}