0
$\begingroup$

I have the function:

Energia[τ_, g_, NN_, L_, vars : {__}] /; Length[vars] == NN/2 := 1/π Sum[
   (2 π)/L (-2 Cos[(2 π)/(L m)] + τ) Cos[vars[[m]]]^2,
  {m, 1, NN/2}] 

which depends on some parameters and a list of variables. If the list is made of 1 element only, I can evaluate it, expand it and so on without problems. Otherwise, with larger lengths I get the message that says that the function is not a number when evaluated.

In particular, I need to use Nminimize on it to find the minimum over the list and I don't know how to proceed. Do you know how to make it possible to minimize it over an arbitrary list of variables?

Edit: I still have problems even if the condition Length[vars]=NN/2 is satisfied. For example, if I try the following:

    L = 200;
    g = 1;
    energies = List[];
    For[i = 0, i < 200, i++, 
    ss = NMinimize[
    Expand[Energia[0.0 + i  0.05, g, L/4, L, 
    Table[\[Theta][j], {j, 25}]]], Table[\[Theta][j], {j, 25}]];
    AppendTo[energies, ss[[1]]];]

to minimize the function over the list of variables, I get error messages of the form:

NMinimize::nnum: The function value Energia[0,1,50,200,        {0.870932,0.503921,0.558626,0.299901,-     0.304018,0.286253,0.335431,0.081131,0.24267,0.362557,-0.200533,0.645043,<<19>>,0.416675,-0.353446,0.627328,-0.413779,-0.39947,0.782446,-0.484861,-0.165669,0.786984,0.360874,-0.755282,0.188728}] is not a number at {\[Theta][1],\[Theta][2],\[Theta][3],\[Theta][4],\[Theta][5],\[Theta][6],\[Theta][7],\[Theta][8],\[Theta][9],\[Theta][10],\[Theta][11],\[Theta][12],\[Theta][13],\[Theta][14],\[Theta][15],\[Theta][16],\[Theta][17],\[Theta][18],\[Theta][19],\[Theta][20],\[Theta][21],\[Theta][22],\[Theta][23],\[Theta][24],\[Theta][25]} = {0.870932,0.503921,0.558626,0.299901,-0.304018,0.286253,0.335431,0.081131,0.24267,0.362557,-0.200533,0.645043,0.875082,0.416675,-0.353446,0.627328,-0.413779,-0.39947,0.782446,-0.484861,-0.165669,0.786984,0.360874,-0.755282,0.188728}.

and I don't see what I am missing exactly.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

The function Energia will only execute if the length of vars is half the value of NN. So the following example will execute because the length of vars is 8 and NN is 16.

{τ, g, NN, L} = {3, 5, 16, 4};
vars = Range[5, 12];

Energia[τ, g, NN, L, vars]

You seem to have NN = 2 if only a vars list of length 1 will execute.

The Condition in this case is

pattern /; Length[vars] == NN/2
$\endgroup$
2
  • $\begingroup$ Thank you for the answer! I edited above to show that I still get an error message saying that the function is not evaluated to a number even if the condition is satisfied. Do you have any idea why it is happening? $\endgroup$
    – lgotta
    Sep 19, 2021 at 21:36
  • $\begingroup$ I solved the issue, no problem! $\endgroup$
    – lgotta
    Sep 20, 2021 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.