# How to use StepMonitor in NMinimize when using vector variable

Bug persists through 12.0

For example, when I call this code

res = NMinimize[{Abs[{Indexed[qi, {1}] + 2, Indexed[qi, {2}],
Indexed[qi, {3}]}].{1, 2, 1},
Norm[{Indexed[qi, {1}], Indexed[qi, {2}],
Indexed[qi, {3}]} - {0.3, 0, 0.2}] < 10},
qi \[Element] ImplicitRegion[a^2 + b^2 + c^2 < 100, {a, b, c}]]


I get some answer, but I can't print intermediate results with StepMonitor. When I run this code

res = NMinimize[{Abs[{Indexed[qi, {1}] + 2, Indexed[qi, {2}],
Indexed[qi, {3}]}].{1, 2, 1},
Norm[{Indexed[qi, {1}], Indexed[qi, {2}],
Indexed[qi, {3}]} - {0.3, 0, 0.2}] < 10},
qi \[Element] ImplicitRegion[a^2 + b^2 + c^2 < 100, {a, b, c}],
StepMonitor :> Print[Indexed[qi, {1}]]]


I get this error message

Block::dup: Duplicate local variable Indexed found in local variable specification {Indexed,Indexed,Indexed}.


Disclaimer, this is not the real problem I'm solving, but rather a simplified version which shows exact same problem.

This looks like a bug. You can work around the issue by avoiding Indexed (I also needed to include an explicit Method option in M12):$$\in$$

res = NMinimize[
{
Abs[{q1+2,q2,q3}].{1,2,1},
Norm[{q1,q2,q1}-{0.3,0,0.2}]<10
},
{q1,q2,q3} ∈ ImplicitRegion[a^2+b^2+c^2<100,{a,b,c}],
StepMonitor:>Print[{q1,q2,q3}]
]


{0.0556604,-0.287369,0.30879}

{0.0556604,-0.287369,0.30879}

{0.0199391,0.116113,-0.507085}

...

{4.4409*10^-16, {q1 -> -2., q2 -> -1.8128*10^-22, q3 -> -2.07681*10^-22}}

• Thanks, it works. It's a bit ugly since I have a vector with 7 components, but it works – Marko Skakun Nov 3 at 18:48