So the other day I was stuck on a problem because of a typo. Instead of iterating through my nested loop with i += 1 I was using i=+1. I didn't notice this until I started printing the number of steps and saw it was printing step 1 continuously. The plots I was getting therefore weren't making any sense.
However what I don't understand is why I got any plots at all, and the code wasn't stuck in an infinite loop? Also, I should only have been calculating data after halfway through the number of steps, so I don't understand how I had any data at all. Or does i =+ 1 mean something else? I can't seem to find much information on i=+1 at all online
Here's a condensed version of the original code:
for temp in np.linspace(1.0,4.0,num=100):
energyarray = []
for step in np.arange(0, sw*2):
for i in range(n-1):
for j in range(n-1):
H_old = -J*matrix[i,j]*(matrix[i,j-1] + matrix[i,j+1] + matrix[i-1,j] + matrix[i+1,j])
H_new = J*matrix[i,j]*(matrix[i,j-1] + matrix[i,j+1] + matrix[i-1,j] + matrix[i+1,j])
del_H = H_old-H_new
if del_H >= 0:
matrix[i,j] = -matrix[i,j]
elif del_H < 0:
prob = np.exp((del_H)/(temp))
rand = random.random()
if rand < prob:
matrix[i,j] = -matrix[i,j]
else:
matrix[i,j] = matrix[i,j]
if step >= (sw):
Ene = EnergyCal(matrix)
energyarray.append(Ene)
step =+ 1
energy_sum = []
energy_sum = sum(energyarray)
plt.figure(10)
plt.plot(temp, energy_sum, 'ro')
plt.show()