Monte Carlo Simulation for Random Walk in 2D using MATLAB, Exercises of Stochastic Processes

This matlab script demonstrates a monte carlo simulation for a random walk in a 2d plane. The script sets the generator to an initial state and performs 4000 iterations, updating the position of a point based on the generated random numbers. The script also creates a movie with 20 frames showing the point's movement. This script can be used for teaching and learning purposes, particularly in statistics, probability, and computer science courses.

Typology: Exercises

2011/2012

Uploaded on 08/12/2012

ranganath
ranganath 🇮🇳

4.7

(3)

37 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
% Program name: monte_diffusion.m
%
for k_trace = 0: 2
N = 200
imax = 100;
jmax = 100;
nframes = 20; % number of frames for movie
i = 50;
j = 50;
rand('state', k_trace) % this sets the generator to an initial state
for k = 1: 4000
% left = 0; right = 0;
% top = 0; bottom = 0;
for kk = 1: 2
rr = rand;
% if((rr >= 0 ) & ( rr < 0.25) & (right == 1))
if((rr >= 0 ) & ( rr < 0.25) )
i = i + 1;
j = j;
elseif((rr >= 0.25 ) & ( rr < 0.50) )
i = i;
j = j + 1;
elseif((rr >= 0.50 ) & ( rr < 0.75))
i = i - 1;
j = j;
elseif((rr >= 0.75 ) & ( rr <= 1.00))
i = i;
j = j - 1;
end
end
if i >= imax
break
end
if i <= 1
break
end
if j >= jmax
break
end
if j <= 1
break
end
axis([0 imax 0 jmax]);
if k_trace == 0
plot(i, j, 'r:.');
elseif k_trace == 1
plot(i, j, 'b:.');
elseif k_trace == 2
docsity.com
pf2

Partial preview of the text

Download Monte Carlo Simulation for Random Walk in 2D using MATLAB and more Exercises Stochastic Processes in PDF only on Docsity!

% Program name: monte_diffusion.m % for k_trace = 0: 2 N = 200 imax = 100; jmax = 100; nframes = 20; % number of frames for movie i = 50; j = 50; rand('state', k_trace) % this sets the generator to an initial state for k = 1: 4000 % left = 0; right = 0; % top = 0; bottom = 0; for kk = 1: 2 rr = rand; % if((rr >= 0 ) & ( rr < 0.25) & (right == 1)) if((rr >= 0 ) & ( rr < 0.25) ) i = i + 1; j = j; elseif((rr >= 0.25 ) & ( rr < 0.50) ) i = i; j = j + 1; elseif((rr >= 0.50 ) & ( rr < 0.75)) i = i - 1; j = j; elseif((rr >= 0.75 ) & ( rr <= 1.00)) i = i; j = j - 1; end end if i >= imax break end if i <= 1 break end if j >= jmax break end if j <= 1 break end axis([0 imax 0 jmax]); if k_trace == 0 plot(i, j, 'r:.'); elseif k_trace == 1 plot(i, j, 'b:.'); elseif k_trace == 2

docsity.com

plot(i, j, 'g:.'); end hold on F(k) = getframe; i j end end % end of traces

docsity.com