1. The problem statement, all variables and given/known data In a XY - model in two dimensional square lattice. I want to calculate specific heat capacity, <E> and <E^2>. By using the formula: H = -Jsum(<i,j>)Cos(tetha(i)-tetha(j)) The Metropolis algorithm reads: Simulations for Statistical and Thermal Physics 2. Relevant equations H = -Jsum(<i,j>)Cos(tetha(i)-tetha(j)) 3. The attempt at a solution Here is my code for the Hamiltonian: http://s716.photobucket.com/user/Pitoraq/media/MC_zpsqqzwctgg.png.html?sort=3&o=0 What is wrong with my code ?