您当前的位置: 首页 >  matlab

slandarer

暂无认证

  • 1浏览

    0关注

    248博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

高尔顿钉板仿真模拟 MATLAB

slandarer 发布时间:2021-10-17 09:11:03 ,浏览量:1

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

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 完整代码:

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
关注
打赏
1664692598
查看更多评论
立即登录/注册

微信扫码登录

0.0394s