In [28]:
using PyPlot
In [33]:
function int2spin(integer,N)
vec = split(string(bin(integer,N)),"")
vec = map(x->parse(Int,x), vec)
return vec-.5
end
function H(spins, J, h)
N = length(spins)
sum1 = 0
for i in 1:N-1
sum1 += spins[i]*spins[i+1]
end
return -J*sum1-h*sum(spins)
end
function boltz(spins, T, J, h)
k_B = 1
return exp(-H(spins, J, h)/(k_B*T))
end
function M(T, N, J, h)
Z = 0
for i in 0:2^N-1
Z += boltz(int2spin(i,N), T, J, h)
end
sum1 = 0
for i in 0:2^N-1
spins = int2spin(i,N)
sum1 += boltz(spins, T, J, h)*sum(spins)/Z
end
return sum1
end;
In [ ]:
J = 1; N = 14; h_list = [.1 .2 .3 1.]; steps = 101
T_list = linspace(.1,5,steps)
figure(1)
for h in h_list
M_list = Array{Float64}(steps)
for i in 1:steps
M_list[i] = M(T_list[i], N, J, h)
end
plot(T_list, M_list, label="h=$h")
end
legend()
xlabel("Temperatur")
ylabel("Magnetisierung")
show()
In [ ]: