import numpy as np
def run_mc(n_steps, T, k):
"""
This function performs metropolis monte carlo sampling of a harmonic potential at temperature T
and returns the trajectory
"""
[insert]
return traj
import numpy as np
def run_mc(n_steps, T, k):
"""
This function performs metropolis monte carlo sampling of a harmonic potential at temperature T
and returns the trajectory
"""
traj = np.zeros(n_steps)
traj[0] = np.random.uniform(-1, 1)
for i in range(1, n_steps):
x_new = np.random.uniform(-1, 1)
dE = 0.5*k*(x_new**2 - traj[i-1]**2)
if dE < 0:
traj[i] = x_new
else:
if np.random.uniform(0, 1) < np.exp(-dE/T):
traj[i] = x_new
else:
traj[i] = traj[i-1]
return traj
Go to Google Form