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], n_sites)
state_traj[0] = state
for i in range(1, n_steps):
site = np.random.randint(n_sites)
delta_E = 2*state[site]*(J*state[(site+1)%n_sites] + J*state[(site-1)%n_sites] + h)
if delta_E <= 0:
state[site] = -state[site]
elif np.random.rand() < np.exp(-delta_E/T):
state[site] = -state[site]
state_traj[i] = state
return state_traj
Go to Google Form