TransWikia.com

Why do I get the (NMinimize::nrnum) error and how to fix it?

Mathematica Asked on July 18, 2021

I have an energy function which depends on the angles of the electrons({x} symbol for theta) and phase of electrons({y} symbol for phi). I am using NMinimize to minimize the energy with respect to {x}s and {y}s under the two following conditions defined as "constheta" && "consphi". I keep getting the "NMinimize::nrnum" error. Is there any suggestion how to fix it?

  Clear["Global`*"]
 (*Energy depends on theta and phi/ I want to find the values of theta 
   and phi for which energy is minimized*)
   f[[ScriptL]0_Integer?NonNegative] := 
   f[[ScriptL]0] = 
   Module[{ne = 2 [ScriptL]0 + 2, varsphi, varstheta, vars, constheta,
   consphi, mtablep, M, ene},
   varstheta = 
   Array[{Symbol["x$" <> ToString[#]]} &, ne];  (*theta is the angle of the electrons' spins*)
   varsphi = 
   Array[{Symbol["y$" <> ToString[#]]} &, ne];   (*Phi is the phase of the electrons' spins*)
   vars = Flatten[Join[varstheta, varsphi]];
   vars[[1]] = 0;    (* the angle of first electron should be zero*)
   vars[[ne]] = [Pi]/2;   (* the angle of the last electron should be [Pi]/2 *)
   constheta = 
   Apply[And, Thread[0.0 <= vars[[2 ;; ne - 1]] <= [Pi]/2]]; (*Condition on theta*)
   consphi = 
   Apply[And, Thread[0.0 <= vars[[ne + 1 ;; 2 ne]] <= 2 [Pi]]];     (*Condition on phi*)
   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], 0}, {[ScriptL], 
         0}, {[ScriptL]temp, 0}]^2, 
     0], {mval, -[ScriptL]temp, [ScriptL]temp}], 
    {[ScriptL]temp, 0, 2 [ScriptL]}], 0];

  ene[ang_?ArrayQ] := 
  Sum[(Cos[ang[[p1]]] Sin[ang[[pp1 + 1]]] Exp[-I ang[[p1]]] - 
    Sin[ang[[p1 + 1]]] Cos[ang[[pp1]]] Exp[-I ang[[pp1]]] + 
    If[p1 == pp1, 
     Sin[ang[[p1 + 1]]] Cos[ang[[p1]]] Exp[-I ang[[p1]]], 
     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], constheta && consphi}, 
  Drop[vars[[2 ;; 2 ne]], {ne - 1}], 
  Method -> "SimulatedAnnealing"]]

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP