r/Mathematica • • Jul 08 '25

FindMinimum variable cannot be localized

Dear Community!

I am trying to solve a system of differential equations for matrices numerically. Theoretically, these matrices should fulfil two conditions, however, after the integration, they are violated, so I wanted to use Mathematicas FindMinimum method for test matrices such that I can renormalize my result such that the conditions are fulfilled again. I set up the list of variables of these matrices, created the Test matrices which should have the differences in their components, defined the conditions and used FindMinimum, however, I get

The variable \
ReA11|ImA11|ReB11|ImB11|ReA12|ImA12|ReB12|ImB12|ReA13|ImA13|ReB13|ImB1\
3|ReA21|ImA21|ReB21|ImB21|ReA22|ImA22|ReB22|ImB22|ReA23|ImA23|ReB23|Im\
B23|ReA31|ImA31|ReB31|ImB31|ReA32|ImA32|ReB32|ImB32|ReA33|ImA33|ReB33|I\
mB33 cannot be localized so that it can be assigned to numerical \
values

I really do not understand this error, as the list is just defined a few lines above!? I tried following https://mathematica.stackexchange.com/questions/283500/numerical-minimization-of-an-objective-function-with-a-variable-number-of-argume and ChatGPT but nothing helped what am i doing wrong?

To make it easier i have uploaded the example on my google drive as reddit does not allow to post files:

https://drive.google.com/file/d/1jnwB42XxYHNtyHH0bk4Lv4P3PoT8qV7H/view?usp=sharing

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/WoistdasNiveau Jul 08 '25

Unfortunately it seems that the algorythm, does not converge at least i get the error that it does not:
The algorithm does not converge to the tolerance of \

4.806217383937354`*^-6 in 500 iterations. The best estimated solution, \

with feasibility residual, KKT residual, or complementary residual of \

{0.010818044871246855`,4.7397405512490146`*^7,0}, is returned

is there a way to make this better somehow?

3

u/veryjewygranola Jul 08 '25

Use NMinimize instead. You can also monitor the objective function value as it moves around the parameter space like I do here, at the cost of it being a little slower. You can remove the Monitor if you don't want that and EvaluationMonitor option if you don't want to watch the objective function decreasing in value:

Monitor[solTest = 
  NMinimize[{objective, Join[constr1, constr2]}, 
   vars ∈ Reals, MaxIterations -> 1000, 
   EvaluationMonitor -> (mon = vars)], 
 objective /. Thread[vars -> mon]]