📄 onedwalk.m
字号:
%spider.m/created by PJNahin for "Dueling Idiots"(1/10/99)
%This m-file simulates the one-dimensional, symmetrical walk
%with an absorbing barrier at x = 3.
%
rand('state',100*sum(clock)) %new seed for generator;
web1=[1;0;2;2]; %input web as column vectors;
web2=[3;0;1;2];
web3=[0;0;4;3];
web=[web1 web2 web3]; %construct web from vectors;
d=zeros(1,100); %initialize duration vector;
for walks=1:10000
position=1; %put spider at starting position;
steps=0; %initialize number of steps;
while position~=2 %if fly not yet reached, then ...
choices=web(position,1); %how many choices for next step?
choice=floor(choices*rand)+2; %make a choice;
position=web(position,choice);%move to new position;
steps=steps+1; %up-date number of steps;
end
d(steps)=d(steps)+1; %fly reached, so up-date
end %duration vector;
average=0; %compute the average number of
for i=1:50 %steps to reach the fly;
average=average+i*d(i);
end
average/10000
plot(d) %plot duration vector;
title('Distribution of Walk Durations')
xlabel('number of steps')
ylabel('number of walks')
figure(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -