Filter löschen
Filter löschen

Function script for Monte Carlo simulation with varying temperature

10 Ansichten (letzte 30 Tage)
I'm writing a Monte carlo script that will simulate the canonical ensemble from statistical mechanics. I got it working and it shows some nice plots of the energy distribution and everything is as it seems, for low temperatures we have a high occurence of low energy states as hoped but now I want to create a function that runs the script many times for a range of temperatures. Below is the code. My question is can you give me some tip(s) on where to begin? I'm not very experienced with Matlab and especially not with functions. I not really sure where to begin. Thanks.
function [E] = MonteCarlo(T) % Function(Output) = function_name(input)
E_0 = 1; % Max energy state
k = 1; % Boltzmann constant
Beta = 1/(k*T); % Temperature of system
N = 1000; % Length of the sequence
E = zeros(1,N); % Initiate sequence
E(1) = rand * E_0; % Initial energy;
r = rand;
for i = 2:N
e = rand * E_0;
if e <= E(i-1)
E(i) = e;
else
if exp(-(Beta * (e-E(i-1)))) >= r
E(i) = e;
else
E(i) = E(i-1);
end
end
end
  3 Kommentare
John D'Errico
John D'Errico am 26 Feb. 2019
Bearbeitet: John D'Errico am 26 Feb. 2019
You can't just put a for loop around your function, where T varies? That is, call your function multiple times in a loop. Save the results, or do whatever you wish with them. You already know how to use a for loop, so where is the problem?
Tom Edwards
Tom Edwards am 26 Feb. 2019
i want a lot of T values over a range of 2-4 magnitudes potentially.
So i can also specify that T should change in a separate loop or make it part of the function?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Feb. 2019
Bearbeitet: Image Analyst am 27 Feb. 2019
OK, try this:
T = whatever, like from T=linspace(1,10000, 1000000) or T=10000*rand(1, 1000000) or whatever.
for k = 1 : length(T)
E(k) = MonteCarlo(T(k));
end

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by