mathematical optimization – How to fix the following code to optimize the function?

I have previously used this code to find the angles (vector of angles, ang) for which my energy (ene function) is optimized and this code is working properly:

``````f((ScriptL)0_Integer?NonNegative) :=
f((ScriptL)0) =
Module({ne = 2 (ScriptL)0 + 2, vars, cons, mtablep, M, ene},
vars = Array(Symbol("x\$" <> ToString(#)) &, ne);
vars((1)) = 0;
vars((ne)) = (Pi)/2;
cons = Apply(And, Thread(0 <= vars((2 ;; ne - 1)) <= (Pi)/2));
mtablep = Riffle(Range(0, -(ScriptL)0, -1), Range((ScriptL)0));

M((ScriptL)_, p1_, pp1_, p2_, pp2_) :=
If(mtablep((p1)) + mtablep((pp1)) ==
mtablep((p2)) + mtablep((pp2)),
Sum((2 (ScriptL) + 1)^2 (2 (ScriptL)temp + 1)/(4 (Pi)) Sum(
If(mtablep((p1)) + mtablep((pp1)) == mval &&
mtablep((p2)) + mtablep((pp2)) == mval,
ThreeJSymbol({(ScriptL), mtablep((p1))}, {(ScriptL),
mtablep((pp1))}, {(ScriptL)temp, -mval})*
ThreeJSymbol({(ScriptL), mtablep((p2))}, {(ScriptL),
mtablep((pp2))}, {(ScriptL)temp, -mval})*
ThreeJSymbol({(ScriptL), 0}, {(ScriptL),
0}, {(ScriptL)temp, 0})^2,
0), {mval, -(ScriptL)temp, (ScriptL)temp}),
{(ScriptL)temp, 0, 2 (ScriptL)}), 0);

ene(ang_?VectorQ) :=
Sum((Cos(ang((p1))) Sin(ang((pp1 + 1))) -
Sin(ang((p1 + 1))) Cos(ang((pp1))) +
If(p1 == pp1, Sin(ang((p1 + 1))) Cos(ang((p1))),
0)) (Cos(ang((p2))) Sin(ang((pp2 + 1))) -
Sin(ang((p2 + 1))) Cos(ang((pp2))) +
If(p2 == pp2, Sin(ang((p2 + 1))) Cos(ang((p2))),
0)) M((ScriptL)0, p1, pp1, p2, pp2), {p1, 1,
2 (ScriptL)0 + 1}, {p2, 1, 2 (ScriptL)0 + 1}, {pp1, 1,
p1}, {pp2, 1, p2});
{ne, #} & /@
NMinimize({ene(vars), cons}, vars((2 ;; ne - 1)),
Method -> "SimulatedAnnealing"))
``````

`````` (ScriptL)0 = 1
f((ScriptL)0)
``````

I get

``````   {{4, 0.17206861748693228`}, {4, {x\$2 -> 0.3953992190472485`,
x\$3 -> 0.6879102337804962`}}}
``````

Now I want to use the same method for different functions, as an example I have this routine:

``````k((ScriptL)0_Integer?NonNegative) :=
k((ScriptL)0) =
Module({ne = 2 (ScriptL)0 + 2, vars, cons, (Gamma), factorFxn,
ene}, vars = Array(Symbol("x\$" <> ToString(#)) &, ne);
vars((1)) = 0;
vars((ne)) = (Pi)/2;
cons = Apply(And, Thread(0 <= vars((2 ;; ne - 1)) <= (Pi)/2));
(Gamma) =
Join(Table({i, 1}, {i, -(ScriptL)0, (ScriptL)0}),
Table({i, -1}, {i, -(ScriptL)0, (ScriptL)0}));

factorFxn((ScriptL)_, m1_, m2_, p1_, p2_) :=
If((Gamma)((m1, 2)) == (Gamma)((p1, 2)) && (Gamma)((m2,
2)) == (Gamma)((p2, 2)),
If((Gamma)((p1, 1)) - (Gamma)((m1, 1)) == (Gamma)((m2,
1)) - (Gamma)((p2, 1)),
Sum((2 (ScriptL) + 1)^2 Sum(
If((Gamma)((p1, 1)) - (Gamma)((m1, 1)) ==
mval && (Gamma)((m2, 1)) - (Gamma)((p2, 1)) ==
mval, (-1)^((Gamma)((m1, 1)) + (Gamma)((m2, 1)) +
mval) ThreeJSymbol({(ScriptL), -(Gamma)((m1,
1))}, {(ScriptL), (Gamma)((p1,
1))}, {(ScriptL)temp, -mval})
ThreeJSymbol({(ScriptL)temp,
mval}, {(ScriptL), -(Gamma)((m2,
1))}, {(ScriptL), (Gamma)((p2,
1))}) ThreeJSymbol({(ScriptL), 0}, {(ScriptL),
0}, {(ScriptL)temp, 0})^2,
0), {mval, -(ScriptL)temp, (ScriptL)temp}),
{(ScriptL)temp, 0, 2 (ScriptL)}), 0)
, 0);

ene(th1_?VectorQ) :=(*(2 (ScriptL)0 +1)^2*) Sum(
(Cos(th1((p2))) Cos(th1((p1))) +
Cos(th1((p2))) Sin(th1((p1 + 1))) +
Cos(th1((p1))) Sin(th1((p2 + 1))) +
Sin(th1((p2 + 1))) Sin(th1((p1 + 1))) +
If(p1 == p2, Cos(th1((p1))) Sin(th1((p1 + 1))),
0)) factorFxn((ScriptL)0, m1, m2, p1, p2)
, {p1, 1, 2 (ScriptL)0 + 1}, {m1, 1, 2 (ScriptL)0 + 1}, {p2,
1, p1}, {m2, 1, m1}) {ne, #} & /@
NMinimize({ene(vars), cons}, vars((2 ;; ne - 1)),
Method -> "SimulatedAnnealing"))
``````

but k(1) gives me nothing, and I cannot find where the problem is. I will be very thankful if someone can help me fix this.