How to write a Monte Carlo simulation code, by metropolis method, where I have to do trail move based on the acceptance probability?

2 ビュー (過去 30 日間)
J Vard
J Vard 2018 年 2 月 6 日
編集済み: J Vard 2018 年 2 月 6 日
It si a one dimensional move. I have to consider only one particle. I have a funciton of energy U(x)=1/2*(x^2). Consider a starting position of the particle be 'x', and change in the 'x' position for next move, say, 'delx'. It goes on for 'n' steps. The questions are as follows
1. Generate trial moves based on the probability. We have to accept certain moves and reject certain moves, based on the probability (I guessing you know the procedure).
2. Plot the graph of position and energy. (it will be a inverted bell shape. (Simple Harmonic Motion)
Please find below the code I have written. Also, please explain with a new code, in your words.
n=input('Value of n='); delx=input('Value of delx='); x_old=input('Value of x_old='); x=zeros(n,1); x(1)=x_old;
for i=1:n-1
t=rand;
s=rand;
x(i+1)=x(i)+((t-0.5)*delx);
ener_red=((1/2)*k_red*(x.^2)); % ener_red is the "U(x)_red" in the above reduced equation
%sprintf(' Value of i & x(i) & ener_red(i) are %d | %.4f | %.4f',i,x(i),ener_red(i))
if s<min(1,exp(-(ener_red(i+1)-ener_red(i))))
x(i)=x(i+1);
end
ener_red(i)=((1/2)*k_red*(x(i).^2));
figure(1);
plot(x(i),ener_red(i),'k.');
hold on
F=getframe;
end
Thank you.

回答 (0 件)

カテゴリ

Help Center および File ExchangePhysics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by