%Script to do simulation of random walk in 3D space %Keep track of cummlitave distance travelled. %by Nasser Abbasi, CSUF, spring 2008, Mathematics 504 clear all; close all; % % C O N F I G U R A T I O N change as needed % make sure the probabilites add to ONE % I am not going to check for this :) % pxr = 1/6; %probability of right x step pxl = 1/6; %probability of left x step pyr = 1/6; %probability of right y step pyl = 1/6; %probability of left y step pzup = 1/6; %probability of up z step pzdown = 1/6; %probability of down z step nSteps = 200; %how many steps seed = 10101010; %my seed rand('twister',seed); % % C O N S T A N T S do not change % these are tags for each different step (which matlab % has enumeration data types) % X_RIGHT = 1; X_LEFT = -1; Y_RIGHT = 2; Y_LEFT = -2; Z_UP = 3; Z_DOWN = -3; %try to set limits of plot axis to make plot nice k = .15; % a value choosen to make limits look good XMIN = -round(k*pxl*nSteps); XMAX = round(k*pxr*nSteps); YMIN = -round(k*pyl*nSteps); YMAX = round(k*pyr*nSteps); ZMIN = -round(k*pzdown*nSteps); ZMAX = round(k*pzup*nSteps); % % G E N E R A T E R A N D O M S T E P S % steps = ones(nSteps,X_RIGHT); from = 1; to = round(pxl*nSteps); steps(from:to) = X_LEFT; from = to+1; to = from+round(pyr*nSteps); steps(from:to) = Y_RIGHT; from = to+1; to = from+round(pyl*nSteps); steps(from:to) = Y_LEFT; from = to+1; to = from+round(pzup*nSteps); steps(from:to) = Z_UP; from = to+1; to = from+round(pzdown*nSteps); steps(from:to) = Z_DOWN; indx = randperm(nSteps); steps = steps(indx); % % Start the simulation % lastX = 0; lastY = 0; lastZ = 0; figure; set(gcf,'Resize','off') set(0,'DefaultTextinterpreter','none'); h = title({'',''}); axpos = get(gca,'pos'); extent = get(h,'extent'); set(gca,'pos',[axpos(1) axpos(2) axpos(3) axpos(4)-.20*extent(4)]); set(h,'VerticalAlignment','Middle'); plot3([2*XMIN 2*XMAX],[0 0],[0 0],'r'); text(1.5*XMAX,0,0,'X'); hold on; plot3([0 0],[2*YMIN 2*YMAX],[0 0],'r'); text(0,1.5*YMAX,0,'Y'); plot3([0 0],[0 0],[2*ZMIN 2*ZMAX],'r'); text(0,0,1.5*ZMAX,'Z'); xlim([XMIN XMAX]); ylim([YMIN YMAX]); zlim([ZMIN ZMAX]); view([-37.5 -30]); for i=1:nSteps switch steps(i) case X_RIGHT, currentX=lastX+1; currentStep='X RIGHT'; currentY=lastY; currentZ=lastZ; case X_LEFT, currentX=lastX-1; currentStep='X LEFT'; currentY=lastY; currentZ=lastZ; case Y_RIGHT, currentX=lastX; currentY=lastY+1; currentStep='Y RIGHT'; currentZ=lastZ; case Y_LEFT, currentX=lastX; currentY=lastY-1; currentStep='Y LEFT'; currentZ=lastZ; case Z_UP, currentX=lastX; currentY=lastY; currentZ=lastZ+1; currentStep='Z UP'; case Z_DOWN, currentX=lastX; currentY=lastY; currentZ=lastZ-1; currentStep='Z DOWN'; end plot3([lastX currentX currentX],[lastY lastY currentY],... [lastZ currentZ currentZ],'-ko','MarkerSize',2,... 'MarkerFaceColor','g'); axis off; view([-37.5 -30]); lastX = currentX; lastY = currentY; lastZ = currentZ; line1=sprintf('3D random walk, Step=[%d], distance from origin=[%6.4f]',... i,sqrt(currentX^2+currentY^2+currentZ^2)); line2=sprintf('current step=%10s',currentStep); title(char(line1,line2),'fontsize',12,'interpreter','latex'); drawnow; pause(0.25); end