蒙提霍儿问题条件概率实验.docx
- 文档编号:3401212
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:21
- 大小:18.47KB
蒙提霍儿问题条件概率实验.docx
《蒙提霍儿问题条件概率实验.docx》由会员分享,可在线阅读,更多相关《蒙提霍儿问题条件概率实验.docx(21页珍藏版)》请在冰豆网上搜索。
蒙提霍儿问题条件概率实验
程序1.1.2.1
%三门问题的原始程序
x=randperm(3);%产生随机矩阵x用于车和羊的随机排列,3代表车,1,2代表羊%
y=randperm(3);%产生随机矩阵用于模拟选手的选择,3代表选择,1,2代表不选择%
ify
(1)==3
disp('选择1号门');
a=x
(1);%将初始选择的门后事物赋值到a,a可能为1,2,3%
ifx
(2)==3
x(3)=0;disp('打开3号门');
elseifx(3)==3
x
(2)=0;disp('打开2号门');
else
k=randperm
(2);%此时相当于选手选中了车,主持人会随机打开剩下的两扇门之一%
ifk
(1)==1
x
(2)=0;disp('打开2号门');
else
x(3)=0;disp('打开3号门');
end
end
elseify
(2)==3
disp('选择2号门');
a=x
(2);
ifx
(1)==3
x(3)=0;disp('打开3号门');
elseifx(3)==3
x
(1)=0;disp('打开1号门');
else
k=randperm
(2);
ifk
(1)==1
x
(1)=0;disp('打开1号门');
else
x(3)=0;disp('打开3号门');
end
end
else
disp('选择3号门');
a=x(3);
ifx
(1)==3
x
(2)=0;disp('打开2号门');
elseifx
(2)==3
x
(1)=0;disp('打开1号门');
else
k=randperm
(2);
ifk
(1)==1
x
(1)=0;disp('打开1号门');
else
x
(2)=0;disp('打开2号门');
end
end
end
choose=menu('请作出你的选择','不换门','换门');%让选手作出是否换门的选择%
switchchoose
case1
ifa==3
disp('你中奖了');r=1;%将中奖结果返回到r%
else
disp('很遗憾');r=0;
end
otherwise
ifa~=3
disp('你中奖了');r=0;
else
disp('很遗憾');r=1;
end
end
程序1.1.2.2
n1=0;n2=0;
fori=1:
100000
x=randperm(3);%产生随机矩阵x用于车和羊的随机排列,3代表车,1,2代表羊%
y=randperm(3);%产生随机矩阵用于模拟选手的选择,3代表选择,1,2代表不选择%
ify
(1)==3
disp('选择1号门');
a=x
(1);%将初始选择的门后事物赋值到a,a可能为1,2,3%
ifx
(2)==3
x(3)=0;disp('打开3号门');
elseifx(3)==3
x
(2)=0;disp('打开2号门');
else
k=randperm
(2);%此时相当于选手选中了车,主持人会随机打开剩下的两扇门之一%
ifk
(1)==1
x
(2)=0;disp('打开2号门');
else
x(3)=0;disp('打开3号门');
end
end
elseify
(2)==3
disp('选择2号门');
a=x
(2);
ifx
(1)==3
x(3)=0;disp('打开3号门');
elseifx(3)==3
x
(1)=0;disp('打开1号门');
else
k=randperm
(2);
ifk
(1)==1
x
(1)=0;disp('打开1号门');
else
x(3)=0;disp('打开3号门');
end
end
else
disp('选择3号门');
a=x(3);
ifx
(1)==3
x
(2)=0;disp('打开2号门');
elseifx
(2)==3
x
(1)=0;disp('打开1号门');
else
k=randperm
(2);
ifk
(1)==1
x
(1)=0;disp('打开1号门');
else
x
(2)=0;disp('打开2号门');
end
end
end
ifa==3%不换门时此处用a==3,换门时的程序只需将此处换为a~=3%
disp('你中奖了');r=1;n1=n1+1;%%
else
disp('很遗憾');r=0;n2=n2+1;
end
end
n1
n2
p=n1/100000%返回中奖概率的趋近结果%
程序1.2.2.1
functionwj
x=0;
y=0;
m=0;n=0;
fori=1:
90000
a=0;b=0;c=0;d=0;%有四扇门,a,b,c,d%
x=fix(rand*1000);
x=mod(x,4)+1;
switch(x)
case1
a=1;%a门后有车%
case2
b=1;%b门后有车%
case3
c=1;%c门后有车%
case4
d=1;%d门后有车%
end
y=fix(rand*1000);
y=mod(y,4)+1;
switch(y)
case1%选择a门%
if(a==1)
m=m+1;
else
n=n+1;
end
case2%选择b门%
if(b==1)
m=m+1;
else
n=n+1;
end
case3
if(c==1)%选择c门%
m=m+1;
else
n=n+1;
end
case4
if(d==1)%选择d门%
m=m+1;
else
n=n+1;
end
end
end
m,n%m表示不换门中奖的次数,n表示换门中奖的次数%
程序2.1.3.1
%读取door1的图像
d1=imread('door1.jpg','jpg');
imshow(d1);
程序2.1.3.2
%读取door2的图像%
d2=imread('door2.jpg','jpg');
imshow(d2);
程序2.1.3.3
%读取door3的图像%
d3=imread('door3.jpg','jpg');
imshow(d3);
程序2.1.3.4
%读取汽车的图像%
B=imread('car.jpg','jpg');
imshow(B);
程序2.1.3.5
%读取山羊的图像%
A=imread('goat.jpg','jpg');
imshow(A);
程序2.1.3.6
%读取掌声文件
y=wavread('applause.wav');
wavplay(y);
程序2.1.3.7
functionvarargout=wincar(varargin)
%WINCARM-fileforwincar.fig
%WINCAR,byitself,createsanewWINCARorraisestheexisting
%singleton*.
%
%H=WINCARreturnsthehandletoanewWINCARorthehandleto
%theexistingsingleton*.
%
%WINCAR('CALLBACK',hObject,eventData,handles,...)callsthelocal
%functionnamedCALLBACKinWINCAR.Mwiththegiveninputarguments.
%
%WINCAR('Property','Value',...)createsanewWINCARorraisesthe
%existingsingleton*.Startingfromtheleft,propertyvaluepairsare
%appliedtotheGUIbeforewincar_OpeningFunctiongetscalled.An
%unrecognizedpropertynameorinvalidvaluemakespropertyapplication
%stop.Allinputsarepassedtowincar_OpeningFcnviavarargin.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelpwincar
%LastModifiedbyGUIDEv2.516-Dec-200814:
17:
27
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@wincar_OpeningFcn,...
'gui_OutputFcn',@wincar_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&&ischar(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforewincarismadevisible.
functionwincar_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstowincar(seeVARARGIN)
%Choosedefaultcommandlineoutputforwincar
handles.output=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakeswincarwaitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=wincar_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
%---Executesduringobjectcreation,aftersettingallproperties.
functionfigure1_CreateFcn(hObject,eventdata,handles)
%hObjecthandletofigure1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
functionedit2_Callback(hObject,eventdata,handles)
%hObjecthandletoedit2(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Hints:
get(hObject,'String')returnscontentsofedit2astext
%str2double(get(hObject,'String'))returnscontentsofedit2asadouble
%---Executesduringobjectcreation,aftersettingallproperties.
functionedit2_CreateFcn(hObject,eventdata,handles)
%hObjecthandletoedit2(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
%Hint:
editcontrolsusuallyhaveawhitebackgroundonWindows.
%SeeISPCandCOMPUTER.
ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
程序2.1.3.8
functionvarargout=wingoat(varargin)
%WINGOATM-fileforwingoat.fig
%WINGOAT,byitself,createsanewWINGOATorraisestheexisting
%singleton*.
%
%H=WINGOATreturnsthehandletoanewWINGOATorthehandleto
%theexistingsingleton*.
%
%WINGOAT('CALLBACK',hObject,eventData,handles,...)callsthelocal
%functionnamedCALLBACKinWINGOAT.Mwiththegiveninputarguments.
%
%WINGOAT('Property','Value',...)createsanewWINGOATorraisesthe
%existingsingleton*.Startingfromtheleft,propertyvaluepairsare
%appliedtotheGUIbeforewingoat_OpeningFunctiongetscalled.An
%unrecognizedpropertynameorinvalidvaluemakespropertyapplication
%stop.Allinputsarepassedtowingoat_OpeningFcnviavarargin.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelpwingoat
%LastModifiedbyGUIDEv2.516-Dec-200814:
17:
27
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@wingoat_OpeningFcn,...
'gui_OutputFcn',@wingoat_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&&ischar(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforewingoatismadevisible.
functionwingoat_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstowingoat(seeVARARGIN)
%Choosedefaultcommandlineoutputforwingoat
handles.output=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakeswingoatwaitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=wingoat_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
%---Executesduringobjectcreation,aftersettingallproperties.
functionfigure1_CreateFcn(hObject,eventdata,handles)
%hObjecthandletofigure1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
%
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 蒙提霍儿 问题 条件 概率 实验