import numpy as np
def run_mc(n_steps, n_sites, T, J=1, h=0):
"""
This function performs metropolis monte carlo sampling of the 1d ising model at temperature T
and returns the trajectory
"""
[insert]
return state_traj
import numpy as np
def run_mc(n_steps, n_sites, T, J=1, h=0):
"""
This function performs metropolis monte carlo sampling of the 1d ising model at temperature T
and returns the trajectory
"""
state_traj = np.zeros((n_steps, n_sites))
state = np.random.choice([-1, 1], size=n_sites)
state_traj[0, :] = state
for i in range(1, n_steps):
site = np.random.randint(0, n_sites)
delta_E = 2 * state[site] * (J * (state[(site + 1) % n_sites] + state[(site - 1) % n_sites]) + h)
if delta_E <= 0:
state[site] *= -1
elif np.random.rand() < np.exp(-delta_E / T):
state[site] *= -1
state_traj[i, :] = state
return state_traj
Go to Google Form