📄 start.m
字号:
function Start
%When the user press 'Start' button, this function will be executed. It is
%the main function of the whole program
global PAUSE
global H_axes1
global H_axes2
PAUSE=0.5; %Set the default value of PAUSE 0.5
%Get the ball number and the left-probability from the user
ballnum=str2num(get(findobj(gcbf,'Tag','edit1'),'string'));
leftpro=str2num(get(findobj(gcbf,'Tag','edit2'),'string'));
%If the inputs are not correct, warn the user!
if isempty(ballnum)==1 | ballnum<=0
set(findobj(gcbf,'Tag','text1'),'string','Input correct number!'); %Warning!
pause(2)
set(findobj(gcbf,'Tag','text1'),'string','');
return
end
if leftpro<0 | leftpro>1 | isempty(leftpro)==1
set(findobj(gcbf,'Tag','text2'),'string','Input correct left-probability!'); %Warning!
pause(2)
set(findobj(gcbf,'Tag','text2'),'string','');
return
end
%a and b matrix represent the location of the nails
a=[-1,0,1,2,3,4,5,1,2,3,4,1.5,2.5,3.5,2,3,2.5,6];
b=[-1,0,0,0,0,0,0,1,1,1,1,2,2,2,3,3,4,6];
%Set the location of the frame
a1=[1,1];a2=[2,2];a3=[3,3];a4=[4,4];
b1=[0,1];
a5=[1.5,1.5,0,0,5,5,3.5,3.5];
b5=[5,4,1,0,0,1,4,5];
%Prepare for drawing a ball
s=0:pi/20:2*pi;
r=1/2.3; %The radius of the ball is r=1/2.3
%At the beginning, set the actual number and the theory number of balls in each of the five colum '0'
ballcount=zeros(1,5);
theory=zeros(1,5);
H_waitbar=waitbar(0,'Processing...'); %Produce a wait-bar
for j=1:ballnum
x0=2.5; %Record the initial x-label-value of the ball
y0=4.5; %Record the initial y-label-value of the ball
plot(H_axes1,a,b,'k.',a1,b1,a2,b1,a3,b1,a4,b1,a5,b5,'y',x0+cos(s)*r,y0+sin(s)*r,'MarkerSize',10,'LineWidth',3); %Draw the ball when it is on the top
set(H_axes1,'XTick',[],'YTick',[]);
pause(PAUSE); %Pause to look more clearly
for i=1:4
k=rand(1,1); %Generate four randam numbers which are between 0 and 1
if k<=leftpro
x0=x0-0.5; %If k is smaller than the left-probability, move the ball left
else %Otherwise, move the ball right
x0=x0+0.5;
end
y1=y0-r;
y0=y0-1;
%Draw the ball when it moves between two nails
plot(H_axes1,a,b,'k.',a1,b1,a2,b1,a3,b1,a4,b1,a5,b5,'y',x0+cos(s)*r,y1+sin(s)*r,'MarkerSize',10,'LineWidth',3);
set(H_axes1,'XTick',[],'YTick',[]);
pause(PAUSE);
%Draw the ball when it hits the second nail
plot(H_axes1,a,b,'k.',a1,b1,a2,b1,a3,b1,a4,b1,a5,b5,'y',x0+cos(s)*r,y0+sin(s)*r,'MarkerSize',10,'LineWidth',3);
set(H_axes1,'XTick',[],'YTick',[]);
pause(PAUSE);
end
%Record the actual number of the balls in each colum, and then output them
ballcount(x0+0.5)=ballcount(x0+0.5)+1;
set(findobj(gcbf,'Tag','text18'),'string',j);
set(findobj(gcbf,'Tag','text3'),'string',ballcount(1));
set(findobj(gcbf,'Tag','text4'),'string',ballcount(2));
set(findobj(gcbf,'Tag','text5'),'string',ballcount(3));
set(findobj(gcbf,'Tag','text6'),'string',ballcount(4));
set(findobj(gcbf,'Tag','text7'),'string',ballcount(5));
%Record the theory number of the balls in each colum with the method of binomial distribution
for i=1:5
c(i)=MyCombination(4,i-1);
theory(i)=c(i)*leftpro^(4-i)*(1-leftpro)^i*j; %'MyCombination' is a self-define function that calculates the combination number
end
%And then output them
set(findobj(gcbf,'Tag','text12'),'string',theory(1));
set(findobj(gcbf,'Tag','text13'),'string',theory(2));
set(findobj(gcbf,'Tag','text14'),'string',theory(3));
set(findobj(gcbf,'Tag','text15'),'string',theory(4));
set(findobj(gcbf,'Tag','text16'),'string',theory(5));
%Calculate the frequency of each colum as time goes on
for m=1:5
ballratio(m)=ballcount(m)/j;
end
%Output the frequency with the form of histogram
bar(H_axes2,[0.5:4.5],ballratio);
axis(H_axes2,[-1,6,0,1.2]);
set(H_axes2,'XTick',[]);
%Refresh the wait-bar as time goes on
waitbar(j/ballnum,H_waitbar,'Processing...');
end
%Delete the wait-bar
waitbar(1,H_waitbar,'Finished !');
pause(2);
delete(H_waitbar);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -