四种分布 随机数拟合C+MATLAB.docx
- 文档编号:5213528
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:14
- 大小:164.08KB
四种分布 随机数拟合C+MATLAB.docx
《四种分布 随机数拟合C+MATLAB.docx》由会员分享,可在线阅读,更多相关《四种分布 随机数拟合C+MATLAB.docx(14页珍藏版)》请在冰豆网上搜索。
四种分布随机数拟合C+MATLAB
四种函数的拟合
1正态分布
#include
//#include"iomanip.h"
#include"iomanip"
#include
#include
usingnamespacestd;
constintA=2045;
constintC=1;
constintM=1048576;//2的20次方
doublejunyun(doublea,doubleb,longint*seed)//采取的方法是混合同余法其中A,C,M都是自定义的常数
{
doublet;
*seed=A*(*seed)+C;
*seed=(*seed)%M;
t=(*seed)/(double)M;
t=a+(b-a)*t;
returnt;
}
doublegauss(doublemean,doublesigma,longint*s)
{
inti;
doublex,y;
for(x=0,i=0;i<12;i++)
x+=junyun(0.0,1.0,s);
x=x-6.0;
y=mean+x*sigma;
returny;
}
intmain()
{
inti,j,num;
doublex,mean,sigma;
longints;
cout<<"输入均值和方差的值"< cin>>mean>>sigma; cout<<"输入随机种子: "< cin>>s; cout<<"输入生成随机数个数: "< cin>>num; ofstreamcout("zhengtai.txt"); for(i=0;i { x=gauss(mean,sigma,&s); cout< cout<<""; } return0; 运行程序: } 正态分布拟合图 2均匀分布 #include #include #include #include #include #include //#include"graphics.h" floata,b,c; intn,type; floatnextGaussian(floata,floatb) { floatv0,v1,v2,s; floatnextNextGaussian; //inthaveNextNextGaussian=0; do {do { v1=2*(((float)rand()/RAND_MAX))-1;//between-1.0and1.0 v2=2*(((float)rand()/RAND_MAX))-1;//between-1.0and1.0 s=v1*v1+v2*v2; }while(s>=1||s==0); floatmultiplier=float(sqrt(-2*log(s)/s)); nextNextGaussian=v2*multiplier; //haveNextNextGaussian=1; v0=v1*multiplier;//均值为0,方差为1的标准正态分布 }while(s>=1||s==0); returnv0*b+a;//产生均值为a,方差为b的随机信号 } floatAverageRandom(floatmin,floatmax) { floatc1; c1=(((float)rand()/RAND_MAX))*(max-min)+min; returnc1; } floatsl(intn,inttype,floata,floatb) { floata1,b1; a1=a;b1=b; if(type==0) { c=AverageRandom(a1,b1);//产生均匀分布U(a,b) returnc; } elseif(type==1) { c=nextGaussian(a1,b1);//产生高斯分布N(a,b) returnc; } } voidmain() {intdet; int*count; floatbefore,next; float*xplot; float*s; FILE*outfile; srand(time(0)); cout<<"请输入产生的信号类型--0: 平均分布,1: 高斯分布: "; cin>>type; if(type==1) { cout<<"请输入产生点的均值和方差: "; cin>>a>>b; } else {cout<<"请输入产生点范围: "; cin>>a>>b; } cout<<"请输入产生点的个数: "; cin>>n; s=newfloat[n]; outfile=fopen("output.txt","w"); for(inti=0;i {c=sl(n,type,a,b); fprintf(outfile,"%f",c); s[i]=c; } cout<<"请输入测试的精确度1--1000: "; cin>>det; count=newint[det]; xplot=newfloat[det]; FILE*outfile1; FILE*outfile2; outfile1=fopen("count.txt","w"); outfile2=fopen("xplot.txt","w"); if(type==0)//如果是均匀分布的话 {before=a; next=a+(b-a)/det; for(intk=0;k {count[k]=0; for(intj=0;j {if(s[j]>before&&s[j] count[k]=count[k]+1; } xplot[k]=(before+next)/2; before=before+(b-a)/det; next=next+(b-a)/det; } } else//如果是高斯分布的话 {a=a-3*b; b=a+6*b; before=a; next=a+(b-a)/det; for(intk=0;k {count[k]=0; for(intj=0;j { if(s[j]>before&&s[j] count[k]=count[k]+1; } xplot[k]=(before+next)/2; before=before+(b-a)/det; next=next+(b-a)/det; } } } 运行程序: 均匀分布数据直方图 3泊松分布 #include #include #include #include #include doubleU_Random();//均匀随机数0到1 intpossion(intLambda);//泊松分布随机数 main() { doubleu; floata; intLambda;//可以改为需要的目标平均值 inti; intn; float*s; FILE*outfile; printf("Lambda=\n"); scanf("%d",&Lambda); srand((unsigned)time(NULL));//种子 u=U_Random(); printf("n=\n"); scanf("%d",&n); s=newfloat[n]; outfile=fopen("output.txt","w"); for(i=0;i { a=possion(Lambda); fprintf(outfile,"%f",a); s[i]=a; } delete[]s; } intpossion(intLambda)/*generatepoissonrd,withmean=Lamda*/ { intk=0; doublep=1.0; doublel,u; l=exp(-Lambda);/*exp(-Lambda)isdicimalzero*/ //printf("l=%.15lf\n",l); while(p>=l) { u=U_Random(); p*=u; k++; } returnk-1; } doubleU_Random()/*0to1rd*/ { doublef; f=(double)(rand()%1001); returnf/1000.0; } 运行程序: 泊松分布拟合图 4指数分布 #include //#include"iomanip.h" #include"iomanip" #include #include usingnamespacestd; constintA=2045; constintC=1; constintM=1048576;//2的20次方 doublejunyun(doublea,doubleb,longint*seed)//采取的方法是混合同余法其中D,C,M都是自定义的常数 { doublet; *seed=A*(*seed)+C; *seed=(*seed)%M; t=(*seed)/(double)M; t=a+(b-a)*t; returnt; } doubleexponent(doublebeta,longint*s) { doubleu,x; u=junyun(0.0,1.0,s); x=-beta*log(u); returnx; } intmain() { inti,j,num; doublex,beta; longints; cout<<"输入均值μ: "< cin>>beta; cout<<"输入随机种子: "< cin>>s; cout<<"输入生成随机数个数: "< cin>>num; ofstreamcout("zhishu.txt"); for(i=0;i { x=exponent(beta,&s); cout< } return0; } 运行程序: 指数分布直方图 指数分布曲线图
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 四种分布 随机数拟合C+MATLAB 分布 随机数 拟合 MATLAB
![提示](https://static.bdocx.com/images/bang_tan.gif)