1
$\begingroup$

Following is my code, you can see that in eq[], its arguments are defined by t and list{xs}, If I expand the list{xs}, I have to write a lot of λ. Could you help me to simplify the definition of eq[]? When I wanna set λ from λ1 to λ1000, how can I achieve that?

Clear[n, λs, xs, tvalues]
n = 5;
λs = Symbol["λ" <> ToString[#]] & /@ Range[n](*number of sites*);
xs = Append[λs, β];
tvalues = Range[0., 1., 0.01] (*set of t values*);
randnum = RandomReal[1, {10, n + 1}];

Clear[eq]
eq[t_, {λ1_, λ2_, λ3_, λ4_, λ5_, β_}] = 
  Block[{U = 1., μ = 0.5, ϵ = (-2 t - 0.5), EE, summand},

   EE[k_] := (-2  t ( Cos[(2 π  (k - 1))/n])) - μ;
   summand[λ_, k_] := (U/(2 n)) (Cosh[λ]^2 Sinh[λ]^2 + 
        2 Sinh[λ]^4 + 
        2 β^2 Cosh[λ] Sinh[λ] + 
        4 β^2 Sinh[λ]^2 ) + Sinh[λ]^2 EE[k];
   Total[MapIndexed[summand[#, #2[[1]]] &, λs]] + ( β^2 ϵ) + (U/(2 n)) (β^4)
 ];

```
$\endgroup$
1
  • $\begingroup$ Use constructs of the form x[i] instead of forcing x1, x2,... with ToString. The let the function accept a List argument, instead of specifying the elements of the list explicitly in the argument pattern. $\endgroup$ Commented May 18, 2020 at 18:00

1 Answer 1

1
$\begingroup$

Define your function using the following (note also the :=):

eq[t_, {lambdas__, β_}] := (* rest of your code *)

then inside the code change λs in MapIndexed[summand[#, #2[[1]]] &, λs]] to

MapIndexed[summand[#, #2[[1]]]&, List[lambdas]]]
$\endgroup$
1
  • $\begingroup$ Thanks, it is working. $\endgroup$
    – Tim
    Commented May 18, 2020 at 20:35

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.