All pastes #31904 Raw Edit

Untitled

public text v1 · immutable
#31904 ·published 2005-12-01 00:02 UTC
rendered paste body
 %sailor drunk man
 %Darvell Powell
 
 %Clear data and command window, standard procedure...
 clc
 clear
 
%Constant Probability
p=0.093

%Number of steps nessesary
n=12

%Range for planks (y values)
ya=[0:n:n] %Length of 12 (steps)

%First plank
xa=[-1,-1]
%Second Plank
xb=[1,1]
%Third Plank
xc=[3,3]
%Fourth Plank
xd=[5,5]
%Fifth Plank
xe=[7,7]
%Sixth Plank
xf=[9,9]
%Seventh Plank
xg=[11,11]


%Plot the planks
axis([-4 15 -4 15])
%axis([xa(1)-2, xb(1)+2, ya(1)-2, n+2])
plot(xa,ya,xb,ya,xc,ya,xd,ya,xe,ya,xf,ya,xg,ya) 

#################################################################


% sailor.m
% random walk in 2-D
% does the drunken sailor make it to the boat?

n = input('Number of walks to simulate   ');
nsafe = 0;		% number of successes


for i = 1:n     % each new walk
   steps = 0;	% each new walk
   x = 0;
   y = 0;		% initial positions
   % x is along the jetty
   % y is transverse to desired path
   
   while x <= 50 & abs(y) <= 8 & steps < 1000
      steps = steps + 1;
      r = rand;
      if r < 0.6			% 60% chance of going straight
         x = x + 1;
      elseif r < 0.8	% 20% chance of going right
         y = y + 1;		
      else
         y = y - 1;		% 20% chance of going left
      end
   end
   
   if x > 50
      nsafe = nsafe + 1;
   end
   
   end
   
   prob = 100*nsafe/n;
   disp(prob);
   
         
         
%inital position of sailor is
%(5,-3)
x = 5;
y = -3;

while x <= 11 & x >= -1 & y <= 8 & steps < 1000
      steps = steps + 1;
      r = rand;
      if r < 0.6			% 60% chance of going straight
         x = x + 1;
      elseif r < 0.8	% 20% chance of going right
         y = y + 1;		
      else
         y = y - 1;		% 20% chance of going left
      end
   end