Friday, May 18, 2012

Monte-Carlo Simulation of Particles in a Box - Diffusion using Matlab

particle_monte
%=========================================================================
%******** To do Monte Carlo simulation of particle diffusion *************
%************************ By Dr. Mahesha MG ******************************
% Date: 17/11/2012
%=========================================================================

clear
clc
a1=fopen('particle.dat','w'); %Data will be stored in this file
t=0:50000;
nop=10000;  % Total number of particles
x=[nop];
nl=nop;   % Asssumed that all particles are on left side of the box at t=0
for i=1:50000
    if rand()<(nl/nop)
        nl=nl-1;
    else

        nl=nl+1;
    end
    fprintf(a1,'%d\t%d\n',t(i),nl);
    x=[x;nl];
end
y=(nop/2)*(1+exp(-2*t/nop)); %Analytic solution
plot(t,x,'b',t,y,'r')
legend('Monte-Carlo','Analytic')
xlabel('Time in s')
ylabel('No. of particles on left side')
fclose(a1);


      Published with MATLAB® 7.13
-->

2 comments:

  1. Hello, can you tell me where you got the analytic solution from?

    ReplyDelete
  2. Wow, this article is fastidious, my younger sister is analyzing these things, so
    I am going to inform her.

    ReplyDelete