My code runs well up to the final two lines:

analytic[n_, m_, t_] := 
 1/2 (KroneckerDelta[n, m] + (-1)^m *(I)^(n - m)*BesselJ[n - m, 2 t])

matrix[l_, t_] := Table[analytic[n, m, t], {n, l}, {m, l}]

diagonal[l_, t_] := Eigenvalues[matrix[l, t]]

S[l_, t_] := -(Sum[
    diagonal[l, t][[j]] Log[
       diagonal[l, t][[j]]] + (1 - diagonal[l, t][[j]])* 
      Log[1 - diagonal[l, t][[j]]], {j, 1, l}])

xdata = Range[0.1, 1, 0.1]

ydata[l_] := S[l, xdata]

plot[l_] := ListPlot[Transpose[{xdata, ydata[l]}]]

It has a real problem with ydata. I can't even get the dimensions of this list (or at least what should be a list), let alone transpose the two lists. However, a simple scatter graph like

xdata = Range[0, 2 Pi, Pi/6]

ydata[x_, y_] := Sin[xdata + x] + y

plot[x_, y_] := ListPlot[Transpose[{xdata, ydata[x, y]}]]

plot[1, 1]

works no problem and I can't see the difference with what I've done here. I'm new to Mathematica and would really appreciate some help with this!


1 Answer 1


Probem. The code

xdata = Range[0.1, 1, 0.1]
ydata[l_] := S[l, xdata]

feeds a list into the second argument of S. But the function S is not designed to deal with that.

Solution. One can add

S[l_,t_List] := Map[S[l,#]&,t];

as a second definition for S. When calling S with a list as the second argument, this new definition is used. For example, using OP's code plus this new definition for S, we can do


enter image description here

Comments. I propose simplifying the definition for S a little bit:

S[l_,t_] := diagonal[l,t] // -#.Log[#]-(1-#).Log[1-#]&;
S[l_,t_List] := Map[S[l,#]&,t];

This will call diagonal[l,t] only once, rather than 4*l many times in OP's code.

Another observation is that for larger values of l, we get Indeterminate results. Compare

(* {0.0348764,0.111092,0.211485,0.326061,0.447528,
    0.570372,0.690543,0.805309,0.913158,1.01367} *)

(* {Indeterminate,Indeterminate,0.211485 +6.97574*10^-16 I,Indeterminate,0.447528,
    0.570372,0.690543,0.80531,0.91316,1.01367} *)

The problem is that when calling say diagonal[7,0.1], there are several eigenvalues very close to $1$. But this is a separate problem.

  • 1
    $\begingroup$ Thanks very much for this! Totally makes sense. I agree that it works well only up to L=7, a worthwhile observation since I'm in the thermodynamic limit here and am supposed to plot for L=O(10^2). I will approach my supervisors about this and get back if there is a resolution. Thanks again. $\endgroup$ Oct 27, 2022 at 16:15

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.