

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


% 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
plot(i, j, 'g:.'); end hold on F(k) = getframe; i j end end % end of traces