Metropolis algorithm in MC

  • Thread starter Firben
  • Start date
  • Tags
    Algorithm
In summary: The problem with your code is that it is not efficient. The Hamtonsum(1) and Hamtonsum(2) variables should not have a dimension because they are not used. You should plot the Hamiltonian(j) and E(j) values on a graph to see if they are consistent.
  • #1
Firben
145
0

Homework Statement


In a XY - model in two dimensional square lattice. I want to calculate specific heat capacity, <E> and <E^2>. By using the formula:

H = -Jsum(<i,j>)Cos(tetha(i)-tetha(j))

The Metropolis algorithm reads:

Simulations for Statistical and Thermal Physics

Homework Equations


H = -Jsum(<i,j>)Cos(tetha(i)-tetha(j))

The Attempt at a Solution



Here is my code for the Hamiltonian:
http://s716.photobucket.com/user/Pitoraq/media/MC_zpsqqzwctgg.png.html?sort=3&o=0

What is wrong with my code ?
 
Physics news on Phys.org
  • #2
Updated my code:

clear
clc
J = 1;
for N = 1:10000

for M = 1:2
tetha1 = (-1 + (1+1)*rand(4,1))*2*pi;
tetha2 = (-1 + (1+1)*rand(4,1))*2*pi;
for i=1:1:4

Hamton(i,M) = J*(cos((tetha1(i)-tetha2(i))));

end

Hamtonsum = sum(Hamton);

end
DeltaE(N) = Hamtonsum(1) - Hamtonsum(2);
end

Still clueless what to do
 
  • #3
Is pi defined anywhere?
 
  • #4
Pi is build in Matlab, no need to define it
 
  • #5
OK, I've done some C++ but not MatLab.

How about Hamtonsum, Hamtonsum(1) and Hamtonsum(2) ? Should Hamtonsum have a dimension?
 
  • #6
That is not the problem now. It is Hamton(i,M) = J*(cos((tetha1(i)-tetha2(i)))); .
Why should i take the hamiltonian 8 times ?. If tetha = 2*pi*[-1,1] (random numbers).
H = -(cos(tehta(i) - tetha(j) + cos(tetha(i) - etha(j+1)+..)
How do i choose the value tetha(i) ?
 
  • #7
My new code is :

clear
clc
Nsq = 100*100;
N = sqrt(Nsq);
T = linspace(0,2,100);J = 1;

randIndx = randi([1,Nsq],Nsq,1);
randR = rand(Nsq,1);
for j = 1:100
spins=2*pi*rand(N);

for i=1:Nsq
index = randIndx(i);
H = 0;
H = H + cos(spins(mod(index,Nsq) + 1) - spins(mod(index - 1 + N,Nsq) + 1));
H = H + cos(spins(mod(index - 2 + Nsq,Nsq) + 1) - spins(mod(index - 1 + Nsq,Nsq) + 1));
H = -J*H;
%Hamiltonian(j) = -H; Hi = (spins(index)*H)/2;
if(Hi <= 0 || randR(i) < (spins(index)*H)/2 )
spins(index) = -spins(index);
end

E(j) = (1/j)*H;

%plot(T,E,'r');
%plot(T,H);
end

%plot(T,Cv,'b');
end
%Esquared(j) = (1/j).*H.^2
%Cv = (1./T.^2).*((E.^2)-Esquared);

plot(T,E);

what is wrong ?
 

1. What is the Metropolis algorithm in Monte Carlo (MC) simulation?

The Metropolis algorithm is a Monte Carlo simulation method that is used to generate a sequence of random samples from a given probability distribution. It is commonly used in statistical physics, chemistry, and other fields to study the behavior of complex systems.

2. How does the Metropolis algorithm work?

The Metropolis algorithm works by generating a random sample from a proposed probability distribution, and then accepting or rejecting the sample based on a comparison with the current sample. If the proposed sample has a higher probability than the current sample, it is always accepted. If the proposed sample has a lower probability, it is accepted with a probability equal to the ratio of the proposed sample's probability to the current sample's probability. This process is repeated multiple times to generate a sequence of samples that approximate the desired probability distribution.

3. What are the advantages of using the Metropolis algorithm in MC simulation?

One advantage of the Metropolis algorithm is that it is a Markov Chain Monte Carlo (MCMC) method, which means that the generated samples are correlated to each other and can be used to estimate statistical properties of the system being studied. Additionally, the Metropolis algorithm allows for the simulation of complex systems with high-dimensional probability distributions, which would be difficult to simulate using other methods.

4. What are some limitations of the Metropolis algorithm?

One limitation of the Metropolis algorithm is that it can be computationally expensive, as it requires multiple iterations to generate a sequence of samples that accurately represent the desired probability distribution. Additionally, the method may not be suitable for simulating systems with highly correlated variables, as the samples generated may not accurately represent the underlying distribution.

5. How is the Metropolis algorithm used in practice?

The Metropolis algorithm is commonly used in many fields, including statistical physics, chemistry, and machine learning. It is often used in combination with other MCMC methods, such as the Gibbs sampling algorithm, to improve the efficiency and accuracy of the simulations. Researchers may also make modifications to the algorithm, such as incorporating adaptive steps or using different proposal distributions, to better suit their specific research needs.

Similar threads

  • Advanced Physics Homework Help
Replies
4
Views
3K
  • Advanced Physics Homework Help
Replies
5
Views
2K
  • Advanced Physics Homework Help
Replies
0
Views
119
  • Advanced Physics Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Atomic and Condensed Matter
Replies
8
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
939
Back
Top