1Matlab概率论与数理统计一、matlab基本操作1.画图【例01.01】简单画图holdoff;x=0:0.1:2*pi;y=sin(x);plot(x,y,'-r');x1=0:0.1:pi/2;y1=sin(x1);holdon;fill([x1,pi/2],[y1,1/2],'b');【例01.02】填充,二维均匀随机数holdoff;x=[0,60];y0=[0,0];y60=[60,60];x1=[0,30];y1=x1+30;x2=[30,60];y2=x2-30;xv=[00306060300];yv=[03060603000];fill(xv,yv,'b');holdon;plot(x,y0,'r',y0,x,'r',x,y60,'r',y60,x,'r');plot(x1,y1,'r',x2,y2,'r');yr=unifrnd(0,60,2,100);plot(yr(1,:),yr(2,:),'m.')axis('on');axis('square');axis([-2080-2080]);22.排列组合C=nchoosek(n,k):knCC,例nchoosek(5,2)=10,nchoosek(6,3)=20.prod(n1:n2):从n1到n2的连乘【例01.03】至少有两个人生日相同的概率公式计算nnnnNNnNNNNnNNNCnp)1()1(1)!(!1!1365364(3651)365364365111365365365365rsrsrsrs=[20,25,30,35,40,45,50];%每班的人数p1=ones(1,length(rs));p2=ones(1,length(rs));%用连乘公式计算fori=1:length(rs)p1(i)=prod(365-rs(i)+1:365)/365^rs(i);end%用公式计算(改进)fori=1:length(rs)fork=365-rs(i)+1:365p2(i)=p2(i)*(k/365);end;end%用公式计算(取对数)fori=1:length(rs)3p1(i)=exp(sum(log(365-rs(i)+1:365))-rs(i)*log(365));endp_r1=1-p1;p_r2=1-p2;Rs=[20253035404550]P_r=[0.41140.56870.70630.81440.89120.94100.9704]二、随机数的生成3.均匀分布随机数rand(m,n);产生m行n列的(0,1)均匀分布的随机数rand(n);产生n行n列的(0,1)均匀分布的随机数【练习】生成(a,b)上的均匀分布4.正态分布随机数randn(m,n);产生m行n列的标准正态分布的随机数【练习】生成N(nu,sigma.^2)上的正态分布5.其它分布随机数函数名调用形式注释Unidrndunidrnd(N,m,n)均匀分布(离散)随机数binorndbinornd(N,P,m,n)参数为N,p的二项分布随机数Poissrndpoissrnd(Lambda,m,n)参数为Lambda的泊松分布随机数georndgeornd(P,m,n)参数为p的几何分布随机数hygerndhygernd(M,K,N,m,n)参数为M,K,N的超几何分布随机数Normrndnormrnd(MU,SIGMA,m,n)参数为MU,SIGMA的正态分布随机数,SIGMA是标准差Unifrndunifrnd(A,B,m,n)[A,B]上均匀分布(连续)随机数Exprndexprnd(MU,m,n)参数为MU的指数分布随机数chi2rndchi2rnd(N,m,n)自由度为N的卡方分布随机数Trndtrnd(N,m,n)自由度为N的t分布随机数Frndfrnd(N1,N2,m,n)第一自由度为N1,第二自由度为N2的F分布随机数gamrndgamrnd(A,B,m,n)参数为A,B的分布随机数betarndbetarnd(A,B,m,n)参数为A,B的分布随机数lognrndlognrnd(MU,SIGMA,m,n)参数为MU,SIGMA的对数正态分布随机数nbinrndnbinrnd(R,P,m,n)参数为R,P的负二项式分布随机数ncfrndncfrnd(N1,N2,delta,m,n)参数为N1,N2,delta的非中心F分布随机数nctrndnctrnd(N,delta,m,n)参数为N,delta的非中心t分布随机数ncx2rndncx2rnd(N,delta,m,n)参数为N,delta的非中心卡方分布随机数raylrndraylrnd(B,m,n)参数为B的瑞利分布随机数weibrndweibrnd(A,B,m,n)参数为A,B的韦伯分布随机数4三、一维随机变量的概率分布1.离散型随机变量的分布率(1)0-1分布(2)均匀分布(3)二项分布:binopdf(x,n,p),若~(,)XBnp,则{}(1)kknknPXkCpp,x=0:9;n=9;p=0.3;y=binopdf(x,n,p);plot(x,y,'b-',x,y,'r*')y=[0.0404,0.1556,0.2668,0.2668,0.1715,0.0735,0.0210,0.0039,0.0004,0.0000]‘当n较大时二项分布近似为正态分布x=0:100;n=100;p=0.3;y=binopdf(x,n,p);plot(x,y,'b-',x,y,'r*')5(4)泊松分布:piosspdf(x,lambda),若~()X,则{}!kePXkkx=0:9;lambda=3;y=poisspdf(x,lambda);plot(x,y,'b-',x,y,'r*')y=[0.0498,0.1494,0.2240,0.2240,0.1680,0.1008,0.0504,0.0216,0.0081,0.0027](5)几何分布:geopdf(x,p),则1{}(1)kPXkpp(6)超几何分布:hygepdf(x,N,M,n),则{}knkMNMnNCCPXkCx=0:9;p=0.3y=geopdf(x,p);plot(x,y,'b-',x,y,'r*')y=[0.3000,0.2100,0.1470,0.1029,0.0720,0.0504,0.0353,0.0247,0.0173,0.0121]6x=0:10;N=20;M=8;n=4;y=hygepdf(x,N,M,n);plot(x,y,'b-',x,y,'r*')y=[0.1022,0.3633,0.3814,0.1387,0.0144,0,0,0,0,0,0]2.概率密度函数(1)均匀分布:unifpdf(x,a,b),1()0axbfxba其它a=0;b=1;x=a:0.1:b;y=unifpdf(x,a,b);(2)正态分布:normpdf(x,mu,sigma),221()21()2xfxex=-10:0.1:12;mu=1;sigma=4;y=normpdf(x,mu,sigma);rn=10000;z=normrnd(mu,sigma,1,rn);%产生10000个正态分布的随机数d=0.5;a=-10:d:12;b=(hist(z,a)/rn)/d;%以a为横轴,求出10000个正态分布的随机数的频率plot(x,y,'b-',a,b,'r.')(3)指数分布:exppdf(x,mu),11()0xeaxbfx其它x=0:0.1:10;mu=1/2;7y=exppdf(x,mu);plot(x,y,'b-',x,y,'r*')(4)2分布:chi2pdf(x,n),122210(;)2(2)00nxnxexfxnnxholdonx=0:0.1:30;n=4;y=chi2pdf(x,n);plot(x,y,'b');%bluen=6;y=chi2pdf(x,n);plot(x,y,'r');%redn=8;y=chi2pdf(x,n);plot(x,y,'c');%cyann=10;y=chi2pdf(x,n);plot(x,y,'k');%blacklegend('n=4','n=6','n=8','n=10');(5)t分布:tpdf(x,n),122((1)2)(;)1(2)nnxfxnnnnholdonx=-10:0.1:10;n=2;y=tpdf(x,n);plot(x,y,'b');%bluen=6;y=tpdf(x,n);plot(x,y,'r');%redn=10;y=tpdf(x,n);plot(x,y,'c');%cyan8n=20;y=tpdf(x,n);plot(x,y,'k');%blacklegend('n=2','n=6','n=10','n=20');(6)F分布:fpdf(x,n1,n2),112122212112121222(()2)10(;,)(2)(2)00nnnnnnnnxxxfxnnnnnnxholdonx=0:0.1:10;n1=2;n2=6;y=fpdf(x,n1,n2);plot(x,y,'b');%bluen1=6;n2=10;y=fpdf(x,n1,n2);plot(x,y,'r');%redn1=10;n2=6;y=fpdf(x,n1,n2);plot(x,y,'c');%cyann1=10;n2=10;y=fpdf(x,n1,n2);plot(x,y,'k');%blacklegend('n1=2;n2=6','n1=6;n2=10','n1=10;n2=6','n1=10;n2=10');3.分布函数(){}FxPXx【例03.01】求正态分布的累积概率值设2~(3,2)XN,求{25},{410},{2},{3}PXPXPXPX,p1=normcdf(5,3,2)-normcdf(2,3,2)=0.5328p1=normcdf(1,0,1)-normcdf(-0.5,0,1)=0.5328p2=normcdf(10,3,2)-normcdf(-4,3,2)=0.9995p3=1-(normcdf(2,3,2)-normcdf(-2,3,2))=0.69779p4=1-normcdf(3,3,2)=0.5004.逆分布函数,临界值(){}yFxPXx,1()xFy,x称之为临界值【例03.02】求标准正态分布的累积概率值y=0:0.01:1;x=norminv(y,0,1);【例03.03】求2(9)分布的累积概率值holdoffy=[0.025,0.975];x=chi2inv(y,9);n=9;x0=0:0.1:30;y0=chi2pdf(x0,n);plot(x0,y0,'r');x1=0:0.1:x(1);y1=chi2pdf(x1,n);x2=x(2):0.1:30;y2=chi2pdf(x2,n);holdonfill([x1,x(1)],[y1,0],'b');fill([x(2),x2],[0,y2],'b');5.数字特征函数名调用形式注释sortsort(x),sort(A)排序,x是向量,A是矩阵,按各列排序sortrowssortrows(A)A是矩阵,按各行排序meanmean(x)向量x的样本均值varvar(x)向量x的样本方差stdstd(x)向量x的样本标准差medianmedian(x)向量x的样本中位数geomeangeomean(x)向量x的样本几何平均值harmmeanharmmean(x)向量x的样本调和平均值rangerange(x)向量x的样本最大值与最小值的差10skewnessskewness(x)向量x的