Monte Carlo simulation matrix for electron spins - help with conditional sign change!

1 Ansicht (letzte 30 Tage)
Hello,
I'm having a problem writing a code to do a conditional sign flip on a particulr value in a matrix. Forewarn, I am VERY new to Matlab and coding in gerenal.
Here is my code so far:
S = 2*random('Discrete Uniform',2,30,30)-3; to generate the matrix - professor provided
i = random('Discrete Uniform',30);
j = random('Discrete Uniform',30); i and j to pick my value in the matrix which i need to flip the sign - professor provided
Po = ((-410*10^-23)*(sum(S, 'all')) to find the probablity of microstate using Pi = (-410*10^-23)*sum of matrix
A = S to make a new matrix that is same as the starting matrix
A(i,j)=-A(i,j) flip sign
Pn = ((-410*10^-23)*(sum(A, 'all')) new probablity of matrix after flip.
Pn/Po to find probablity ratio
This is the part were i get confused. My professor only wants use to accept a "flip" in sign if Pn/Po is greater then 1 or if its not greater then 1 it has to be greater then random('Uniform',0,1). If Pn/Po does not meet the criteria, then we can not accept the flip in sign and must try a new value (using new i and j) and try again. My professor wants to run this test 10000 in order to simulate the eletron spin flips in a paramagnet during induced magnetization. Is there any way to write it as to make the sign change conditional based on Pn/Po?
I also need help with writing the loop function but if someone can help me with the first part I could (maybe) figure out the rest.

Antworten (1)

Kanishk
Kanishk am 3 Sep. 2024
Hi,
I understand you are trying to flip the sign of an element and accept it based on the mentioned condition. To simulate this 10000 times ‘while’ loop can be used.
The ‘if/else’ condition block can be used to check if the flipped element can be accepted or should be flipped back. Here is the code for same.
S = 2*random('Discrete Uniform',2,30,30)-3;
cnt = 0;
A = S;
while(cnt < 10000)
i = random('Discrete Uniform',30);
j = random('Discrete Uniform',30);
Po = (-410*10^-23)*(sum(S, 'all'));
A(i,j)=-A(i,j);
Pn = (-410*10^-23)*(sum(A, 'all'));
if Pn/Po > random('Uniform',0, 1)
disp(['Accepted index' num2mstr(i) num2mstr(j)])
cnt
else
A(i,j)=-A(i,j);
end
cnt = cnt + 1;
end
Please go through the official MATLAB documentation to learn more about ‘if/else’ and ‘while’.
Hope this helps!
Thanks

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by