I have set up a rather large minimisation problem and it works well, if not for one nuisance. I need to often change the number of variables to be given to NMinimize: clearly it is rather impractical to explicitly write down the whole list of variables. I could not find an efficient way though.

The minimisation set-up looks as follows:

    sol11 = NMinimize[    
    Sum[(prep[[s]] - (funo[#, {0, ome22, ome33}] & /@ (MapThread[
    ide, {pts1, Table[{am11, am22, am33}, 2]}])
    )[[s]])^2, {s, 1, 2}], {am11, am22, am33, ome22, ome33}]

The list "prep" is given as input data, as well as "pts1". There are $2k-1$ variables, the first $k$ starting with "am", the last $k-1$ with "ome".

$k$ could be > 20, and I would like to be able to use a list, instead of typing name variables.

I tried something as

          vars = Array[x, 2*k-1]

hoping this could be used in lieu of

          {am11, am22, am33, ome22, ome33}

and hence I tried to substitute the current expression

           {0, ome22, ome33}


           {0, { Drop[vars, k+1]   }    }

and similarly tried to substitute

           Table[{am11, am22, am33}, 2]     


           Table[Take[vars, 3], 2]     

but I get an error mentioning nonatomic expressions being expected. What is the right way to indicate variables for NMinimise, when the number of variables is large?



I add a working example to clarify my issue.

First a NMinimize example line showing what I try to achieve, that, going from an explicit definition of the list of unknowns {x,y,z}

   NMinimize[{x^2 - (y - 1)^2 - z^3, x^2 + y^2 + z^2 <= 4}, {x, y, z}]


     j = Array[a, 3]
     NMinimize[{j[[1]]^2 - (j[[2]] - 1)^2 - j[[3]]^3, 
                             j[[1]]^2 + j[[2]]^2 + j[[3]]^2 <= 4}, j]

In this case it works: I do not need to add unkwowns, as I could just alter the dimension of the Array j.

Now an example using the same structure of the problem I am solving, first using an explicit list for the unknowns:

      k = 4;
      p = 5;
      pts1 = RandomPoint[Sphere[k], p];
      c = Table[1, k];
      omega = Table[0, k];
      funo[a_, ome_] := 
           Sum[a[[i]]*a[[j]]*Cos[ome[[i]] - ome[[j]]], {i, 1, k}, {j, 1, k}];
      c1 = Table[c, p];
      omega1 = Table[omega, p];
      ide[u_, p_] := u p;
      auxa = MapThread[ide, {pts1, c1}];
      dar = funo[#, omega] & /@ auxa;
      sol11 = NMinimize[
      Sum[(dar[[s]] - (funo[#, {0, ome22, ome33, ome44}] & /@ (MapThread[
        ide, {pts1, 
        Table[{am11, am22, am33, am44}, p]}]))[[s]])^2, {s, 1, 
         p}], {am11, am22, am33, am44, ome22, ome33, ome44}]

which works. Similar to before, I would rather avoid having to type the list

       {am11, am22, am33, am44, ome22, ome33, ome44}

as it could contain many elements.

I have hence tried, as in the previous example, something like

       varsam = Array[l, k];
       varome = Array[m, k - 1];

hoping to be able to use the following

      sol11 = NMinimize[
       Sum[(dar[[s]] - (funo[#, Join[0, varome]] & /@ (MapThread[
              ide, {pts1, Table[varsam, p]}]))[[s]])^2, {s, 1, p}], 
            Join[varsam, varome]]

but, contrary to the previous example, I get an error message.

  • $\begingroup$ Does it work with {0, vars[[k + 1]], vars[[2 k - 1]]} instead of {0, ome22, ome33}? $\endgroup$ – MelaGo Oct 28 at 21:26
  • $\begingroup$ I fear not. But I am just about to add a minimal yet working example, to make it clearer. Thanks $\endgroup$ – Smerdjakov Oct 28 at 21:51
  • $\begingroup$ Is ` ide[u_, p_] = u p` supposed to be ` ide[u_, p_] := u p`? $\endgroup$ – MelaGo Oct 28 at 23:12
  • $\begingroup$ In any case, it appears that you should use Join[{0}, varome] rather than Join[0, varome] $\endgroup$ – MelaGo Oct 28 at 23:32
  • $\begingroup$ Yes you spotted it right, I corrected the expression " ide[u_, p_] = u p` supposed to be ` ide[u_, p_] := u p` $\endgroup$ – Smerdjakov Oct 28 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.