当前位置:文档之家› 随机过程matlab程序

随机过程matlab程序

精心整理基本操作-5/(4.8+5.32)^2area=pi*2.5^2x1=1+1/2+1/3+1/4+1/5+1/6exp(acos(0.3))arr2(:,1:2:3)arr3=[12345678]arr3(5:end)arr3(end)绘图x=[0:1:10];y=x.^2-10*x+15;plot(x,y)x=0:pi/20:2*piy1=sin(x);y2=cos(x);plot(x,y1,'b-');holdon;plot(x,y2,‘k--’);legend(‘sinx’,‘cosx’);x=0:pi/20:2*pi;y=sin(x);figure(1)plot(x,y,'r-')gridon平面的ya=xa;建立Mfunctionifdisp(elseif grade>86.0disp('ThegradeisB.');elseif grade>76.0disp('ThegradeisC.');elseif grade>66.0disp('ThegradeisD.');elsedisp('ThegradeisF.');endendendendendfunction y=func(x)if abs(x)<1y=sqrt(1-x^2);else y=x^2-1;endfunction summ(n)i=1;sum=0;whilei=i+1;endstr=[endsymsxdiff(f)diff((x^2+y^2+z^2)^(1/2),x,2)重积分int(int(x*y,y,2*x,x^2+1),x,0,1)级数symsn;symsum(1/2^n,1,inf)Taylor展开式求y=exp(x)在x=0处的5阶Taylor展开式taylor(exp(x),0,6)矩阵求逆A=[0-6-1;62-16;-520-10]det(A)inv(A)特征值、特征向量和特征多项式A=[0-6-1;62-16;-520-10];lambda=eig(A)[v,d]=eig(A)poly(A)多项式的根与计算p=[10-2-5];r=roots(p)p2=poly(r)y1=polyval(p,4)例子:例子设设Ex=symsum(k*p*(1-p)^(k-1),k,1,inf)symsxy;f=x+y;Ex=int(int(x*y*f,y,0,1),0,1)参数估计例:对某型号的20辆汽车记录其5L汽油的行驶里程(公里),观测数据如下:29.827.628.327.930.128.729.928.027.928.728.427.229.528.528.030.029.129.829.626.9设行驶里程服从正态分布,试用最大似然估计法求总体的均值和方差。

x1=[29.827.628.327.930.128.729.928.027.928.7];x2=[28.427.229.528.528.030.029.129.829.626.9];x=[x1x2]';p=mle('norm',x);muhatmle=p(1),sigma2hatmle=p(2)^2[m,s,mci,sci]=normfit(x,0.5)假设检验例:下面列出的是某工厂随机选取的20只零部件的装配时间(分):9.810.410.69.69.79.910.911.19.610.210.39.69.911.210.69.810.510.110.59.7设装配时间总体服从正态分布,标准差为0.4,是否认定装配时间的均值在0.05的水平下不小于10。

解::%PPT例2symsxy;s=1;t=2;%P34例[p1,p2]%输出ans=0.06710.0000%P40例3.2.1p3=poisspdf(9,12)%输出p3=0.0874%P40例3.2.2p4=poisspdf(0,12)%输出p4=6.1442e-006%P35-37(Th3.1.1)解微分方程%输入:symsp0p1p2;S=dsolve('Dp0=-lamda*p0','Dp1=-lamda*p1+lamda*p0','Dp2=-lamda*p2+lamda*p1', 'p0(0)=1','p1(0)=0','p2(0)=0');[S.p0,S.p1,S.p2]%输出:ans=[exp(-lamda*t),exp(-lamda*t)*t*lamda,1/2*exp(-lamda*t)*t^2*lamda^2]%P40泊松过程仿真%simulate10timesclear;m=10;lamda=1;x=[];fori=1:m%seedend[x',t1']%输出:ans=%输入:N=[];N=[N,0];N=[N,1];elseift<t1(3)N=[N,2];elseift<t1(4)N=[N,3];elseift<t1(5)N=[N,4];elseift<t1(6)N=[N,5];elseift<t1(7)N=[N,6];elseift<t1(8)N=[N,7];elseift<t1(9)N=[N,8];elseift<t1(10)N=[N,9];elseN=[N,10];endendplot(0:0.1:(t1(m)+1),N,'r-')%输出:clear;fori=1:ms=rand(end[x',t1']N=[];N=[N,0];endN=[N,i];endendN=[N,m];endend%输出:%P48非齐次泊松过程仿真%simulate10timesclear;m=10;lamda=1;x=[];for i=1:ms=rand('seed');%exprnd(lamda,'seed',1);setseeds x=[x,exprnd(lamda)];t1=cumsum(x);end[x',t1']N=[];T=[];for t=0:0.1:(t1(m)+1)T=[T,t.^3];%timeisadjusted,cumulativeintensityfunctionist^3.if t<t1(1)N=[N,0];endfor i=1:(m-1)if t>=t1(i)&t<t1(i+1)N=[N,i];endendif t>t1(m)N=[N,m];endendplot(T,N,%outputans=%P50clear;lamda=1;t=input(forrand('state',sum(clock));x=exprnd(lamda);%intervaltimet1=x;while t1<tx=[x,exprnd(lamda)];t1=sum(x);%arrivingtimeendt1=cumsum(x);y=trnd(4,1,length(t1));%rand(1,length(t1));gamrnd(1,1/2,1,length(t1)));frnd(2,10,1,le ngth(t1)));t2=cumsum(y);end[x',t1',y',t2']X=[];m=length(t1);for t=0:0.1:(t1(m)+1)if t<t1(1)X=[X,0];endfor i=1:(m-1)if t>=t1(i)&t<t1(i+1)X=[X,t2(i)];endendifendendclear;rand(n=1;whilex<tifx>=tendendz=binornd(200,0.5,1,n);%generatensalesy=sum(z);above(i)=sum(y>432000);endpro=mean(above)Output:pro=0.3192%%Simulatethelosspro.ForaCompoundPoissonprocess clear;niter=1.0E3;%numberofiterationslamda=1;%arrivingratet=input('Inputatime:','s')below=repmat(0,1,niter);%setupstoragefori=1:niterrand('state',sum(clock));x=exprnd(lamda);%intervaltimen=1;whilex<tx=x+exprnd(lamda);%arrivingtimeifx>=tn=n;elseendendend%计算1到;n=10fork=1:njueduivec(k,1:6)=jueduiveckend%比较相邻的两行n=70;jueduivecn=chushivec0*(P^n)n=71;jueduivecn=chushivec0*(P^n)%Replacethefirstdistribution,Comparingtwoneighbourabsolute-vectorsoncemorechushivec0=[1/61/61/61/61/61/6];P=[0,1/2,1/2,0,0,0;1/2,0,1/2,0,0,0;1/4,1/4,0,1/4,1/4,0;0,0,1,0,0,0,;0,0,1/2,0,0,1/2;0,0,0,0,1,0] ;n=70;jueduivecn=chushivec0*(P^n)n=71;jueduivecn=chushivec0*(P^n)%赌博问题模拟(带吸收壁的随机游走:结束1次游走所花的时间及终止状态)a=5;p=1/2;m=0;while m<100m=m+1;r=2*binornd(1,p)-1;if r==-1a=a-1;elsea=a+1;endifbreak;endend[ma]%%p=q=1/2p=1/2;form=0;a=5;whilem=m+1;if r==-1a=a-1;elsea=a+1;endendif a==0t1=t1+m;m1=m1+1;elset2=t2+m;m2=m2+1;endendfprintf('Theaveragetimesofarriving0and10respectivelyare%d,%d.\n',[t1/m1,t2/m2]);fprintf('Thefrequenciesofarriving0and10respectivelyare%d,%d.\n',[m1/N,m2/N]);%verify:fprintf('Theprobabilityofarriving0anditsapproximaterespectivelyare%d,%d.\n',[5/10,m1/N]);fprintf('Theexpectationofarriving0or10anditsapproximaterespectivelyare%d,%d.\n',[5*(10-5)/(2*p), (t1+t2)/N]);p=1/4;m1=0;m2=0;N=1000;t1=zeros(1,N);t2=zeros(1,N);for n=1:1:Nm=0;a=5;while a>0&a<15m=m+1;r=2*binornd(1,p)-1;if r==-1a=a-1;elsea=a+1;endendif a==0elseendendfprintf(fprintf(%verify:fprintf(fprintf(%输入:clear;P*Q输出:ans=[-p00*lamda+p01*mu,p00*lamda-p01*mu][-p10*lamda+p11*mu,p10*lamda-p11*mu]输入:[p00,p01,p10,p11]=dsolve('Dp00=-p00*lamda+p01*mu','Dp01=p00*lamda-p01*mu','Dp10=-p10*lamda+p11*m u','Dp11=p10*lamda-p11*mu','p00(0)=1,p01(0)=0,p10(0)=0,p11(0)=1')输出:p00=mu/(mu+lamda)+exp(-t*mu-t*lamda)*lamda/(mu+lamda)p01=(lamda*mu/(mu+lamda)-exp(-t*mu-t*lamda)*lamda/(mu+lamda)*mu)/mumu/(mu+lamda)-exp(-t*mu-t*lamda)*mu/(mu+lamda)p11=(lamda*mu/(mu+lamda)+exp(-t*mu-t*lamda)*mu^2/(mu+lamda))/mu%BPATH1Brownianpathsimulation:for…endrandn('state',100)%setthestateofrandnT=1;N=500;dt=T/N;dW=zeros(1,N);%preallocatearrays...W=zeros(1,N);%forefficiencydW(1)=sqrt(dt)*randn;%firstapproximationoutsidetheloop...W(1)=dW(1);%sinceW(0)=0isnotallowedfor j=2:Nendxlabel(ylabel(randn(xlabel(ylabel(randn(M=1000;plot([0,t],[ones(5,1),U(1:5,:)],'r--'),hold off%plot5individualpaths xlabel('t','FontSize',16)ylabel('U(t)','FontSize',16,'Rotation',0,'HorizontalAlignment','right') legend('meanof1000paths','5individualpaths',2)aveerr=norm((Umean-exp(9*t/8)),'inf')%sampleerror%输出:aveerr=0.0504。

相关主题