Random Walk using Matlab

Posted by Admin On Thursday, February 14, 2013 0 comments

x=0;
y=0;
xv=x;
yv=y;

while (abs(x)<10 && abs(y)<10)
    d=randi(4);
    switch d
        case 1       %N=1
            y=y+1;
        case 2       %S=2
            y=y-1;
        case 3       %E=3
            x=x+1;
        otherwise
            x=x-1;
    end

    xv(end+1)=x;
    yv(end+1)=y;
end

figure
axis([-10,10,-10,10])
title(['Random walk tooks ',num2str(length(xv)),' steps']);
hold on
comet(xv,yv);
hold off

0 comments:

Post a Comment