📄 spider.m
字号:
%spider.m/created by PJNahin for "Dueling Idiots"(1/9/99)
%This m-file simulates 10,000 two-dimensional walks of a spider
%on a user-defined web (in the code below, the web is that
%of Figure 19.1).
%
%
rand('state',100*sum(clock)) %new seed for generator;
t1=[3;0;3;1;2;2;3;4;2]; %input web as COLUMN vectors;
t2=[4;0;2;1;3;5;1;1;2];
t3=[7;0;5;0;6;7;6;3;8];
t4=[8;0;8;0;0;0;8;7;0];
t5=[0;0;0;0;0;0;0;9;0];
web=[t1 t2 t3 t4 t5]; %construct web from column vectors;
d=zeros(1,250); %initialize walk 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 random 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('Fig.19.3-Distribution of Walk Durations on the Web (10,000 walks)')
xlabel('number of steps')
ylabel('number of walks')
figure(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -