高尔顿钉板仿真模拟 MATLAB

Posted slandarer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高尔顿钉板仿真模拟 MATLAB相关的知识,希望对你有一定的参考价值。

写了个自认为界面还可以的高尔顿钉版程序,想改颜色自己调嗷
效果:




完整代码:

function galtonNailBD
% fig axes设置
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fig=figure();
screenSize=get(0,'ScreenSize');
fig.Position=[screenSize(1,[3,4])./10,...
    screenSize(4).*0.5,screenSize(4).*0.8];
fig.Name='galtonBySlandarer';
fig.NumberTitle='off';
fig.Resize='off';
fig.MenuBar='none';
ax=axes(fig);
ax.Position=[0 0 1 1];
hold(ax,'on');
ax.XLim=[-6.5 6.5];
ax.YLim=[0 20.5];
ax.XTick=[];
ax.YTick=[];
ax.XColor='none';
ax.YColor='none';
ax.Color=[.1 .1 .1];
%ax.DataAspectRatio=[1 1 1];

% 绘制基础背景
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
plot([1;1]*(-5.5:1:5.5),[0;8].*ones(2,12),'LineWidth',2.5,'Color',[1,1,1].*.8)
scatterSet=[];
for k=1:12
    tX=(1:k)-(1+k)/2;
    tY=(20-k).*ones(1,k);
    scatterSet=[scatterSet;[tX',tY']];
end
scatter(scatterSet(:,1),scatterSet(:,2),60,'filled','CData',[1 1 1].*.8);
plot([-6.5,-6.5,-1.5,-1,-1,-6.5,-6.5,6.5,6.5,1,1,1.5,6.5,6.5],...
     [8,20.5,20.5,19.8,19,8,0,0,8,19,19.8,20.5,20.5,8],...
     'LineWidth',4,'Color',[1,1,1].*.6);
 
% 制作图像句柄
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
scatter(0,20,60,'LineWidth',2,'CData',[140,196,123]./255)
accHdl=scatter([],[],30,'LineWidth',2,'CData',[140,196,123]./255);
pathHdl=plot(-10,-10,'LineWidth',2,'Color',[181,200,219]./255);
arrowHdl=text(-10,7.2,'↓','HorizontalAlignment','center','FontSize',40,'Color',[181,200,219]./255);
kerHdl=plot(-10,-10,'LineWidth',3,'Color',[180,64,71]./255);

% 模拟实验
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
X=-6:6;Y=zeros(1,13);ballSet=[];
N=100;
for n=1:N
    xyBegin=[0,19.5];fullPath=[0,20;xyBegin];
    tPath=randi(2,[1,12])-1.5;
    for i=1:length(tPath)
        xyBegin=xyBegin+[tPath(i),-0.5];
        fullPath=[fullPath;xyBegin];
        xyBegin=xyBegin+[0,-0.5];
        fullPath=[fullPath;xyBegin];
    end
    % 绘制路径
    pathHdl.XData=fullPath(:,1);
    pathHdl.YData=fullPath(:,2);
    arrowHdl.Position(1)=xyBegin(1);
    
    % 绘制下方堆积小球
    Y=Y+(xyBegin(1)==X);
    ballSet=[ballSet;xyBegin(1),Y(xyBegin(1)==X).*0.3];
    accHdl.XData=ballSet(:,1);
    accHdl.YData=ballSet(:,2);
    
    % 绘制核密度曲线
    [yi,xi]=ksdensity(ballSet(:,1));
    kerHdl.XData=xi;
    kerHdl.YData=yi.*n.*0.3;
    
    
    drawnow
    pause(0.5)
end
disp(ballSet(:,1)')
disp(ballSet(:,2)')

end

以上是关于高尔顿钉板仿真模拟 MATLAB的主要内容,如果未能解决你的问题,请参考以下文章

用Python模拟高尔顿钉板实验

高尔顿钉板与二项分布

雷达检测基于matlab模拟海洋监视雷达检测仿真含Matlab源码 2268期

力学基于matlab立铣刀力模拟仿真含Matlab源码 193期

雷达通信基于matlab雷达仿真模拟系统含Matlab源码 150期

预测模型基于matlab离散状态空间模型模拟预测控制仿真系统(单输入单输出)含Matlab源码 1537期