1
이 코드는 확률 분포 psi_0_x_squared를 생성합니다. 그런 다음이 확률에 따라 마코프 체인 시뮬레이션을 수행합니다. 이 확률 psi_0_x_squared는 실제로 에너지 레벨 n = 0에 대한 위치 x에있을 확률입니다. 이 확률에 따라 x 1000 번 이동 한 후에 위치 x의 히스토그램을 생성하려고합니다. (위치 주파수)내가 선택한 기본 '멋진'대신 높이에 따라 색상이있는 막대 그래프를 출력하도록이 코드를 변경할 수 있습니까
''' Markov-chain Monte Carlo algorithm for a particle in a Gaussian potential,
using the Metropolis algorithm. '''
import math, matplotlib.pyplot as plt, random
def probability(x):
#wavefunction n=0 evaluated at position x
psi_0_x=math.exp(-x ** 2/2.0)/math.pi ** 0.25
#probability n=0 to be at position x
psi_0_x_squared= psi_0_x**2
return psi_0_x_squared
data_x=[0]
x = 0.0 #starts at position 0
delta = 0.5 #stepsize
for k in range(1000): #for this number of trials
x_new = x + random.uniform(-delta, delta) #I displace it a distance delta
if random.uniform(0.0, 1.0) < probability(x_new)/probability(x):
x = x_new
data_x.append(x)
#histogram
cm = plt.cm.get_cmap('cool')
n, bins, patches= plt.hist(data_x, bins=100, normed=True, color='k')
bin_centers = 0.5 * (bins[:-1] + bins[1:])
col = bin_centers - min(bin_centers)
col /= max(col)
for c, p in zip(col, patches):
plt.setp(p, 'facecolor', cm(c))
plt.show()