n =16;
J = 1;
h = 1;
var('T,h');
spinConfig = Tuples([-1,1],n).list()
def Ham(spins,J,h):
total = J*sum(spins[x]*spins[x+1] for x in range(0,len(spins)-1)) + -h * sum(spin for spin in spins)
return total
def Partition(spinConfig,T):
return sum(e^(-Ham(spins,J,h)/T) for spins in spinConfig)
T = 1
mag = (1/n)*derivative(-T*log(Partition(spinConfig,T)),h)
plot(mag(h),(-10,10))