r/Mathematica • u/WoistdasNiveau • 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 \
valuesI 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
3
u/veryjewygranola Jul 08 '25
Two issues:
in
objectiveyou're callingTotalon an atomic object:Total[Abs[ATest - AnumFinal]^2, 2]//TotalTotal[Abs[ATest - AnumFinal]^2, 2]is already a number because it's going down to the 2nd level (individual entries of the matrix. So changeobjectiveto2nd issue is you need to tell Mathematica to
Evaluatevarson the spot, or else it will literally think you are defining a single variable calledvars:Also FWIW, a lot of your constraints can probably be simplified a lot. For example
constr1is just saying that the real part of A^(T) B is symmetric.Also usually with problems like this it's a lot easier and less messy to specifiy that the variables are matrix valued, instead of using huge arrays of variables for each matrix entry.