⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 马尔可夫链.txt

📁 在即算机上模拟一离散状态均匀马尔可夫链
💻 TXT
字号:
1、在计算机上模拟一离散状态均匀马尔可夫链,状态数为2,此马尔可夫链的一步状态转移矩阵为:
                 
解:实现程序如下:
# include <time.h> 
# include<stdlib.h>
# include<string.h>
# include <stdio.h>
# define NUM 10000
void main()
{
	FILE *fp;
	if((fp=fopen("D:\\work\\markov.txt","wb"))==NULL)
	//为了方便观察本程序保存了10000个马尔可夫链的状态在文件markov.txt中
	{
		printf("file open error.\n");
		exit(0);
	}
    double rate;
	char buff[50];
	rate=0.0;

	srand( (unsigned)time( NULL ) );
	int status, temp;
	//产生第一个初始状态,0表示状态0,1表示状态1
	status=0;
	temp=rand();
	temp=temp%2;
	if(temp==0)
	{
		status=0;
		rate+=1.0;
	}
	else
	{
		status=1;
	}

	for(int i = 1; i<NUM; i++)
	{
		temp=(int)(100*rand()/RAND_MAX+1.0);
		//用算法j=(int)(n*rand()/(RAND_MAX+1.0))来产生0到n之间的随机数
		if(status==0)
		{
			fprintf(fp,"0");
			if(temp<60)//状态0以概率0.6转移到状态0
				rate+=1.0;
			else
			{
                status=1;//状态0以概率0.4转移到状态0
				rate+=1.0;
			}
		}
		else if(status==1)
		{
			fprintf(fp,"1");
			if(temp<20)//状态1以概率0.2转移到状态0, 状态1以概率0.8转移到状态1
			    status=0;
		}
	}
	if(status==0)
	{
		fprintf(fp,"0");
		rate+=1.0;
	}
	else if(status==1)
		fprintf(fp,"1");
	rate/=double(NUM);
	printf("The rate of 0 is %f,the rate of 1 is %f\n",rate,1.0-rate);
	fprintf(fp,"\nThe rate of 0 is");
	_gcvt(rate,25,buff);
	fprintf(fp,"%s\n",buff);
	fprintf(fp,"The rate of 1 is");
	_gcvt(1.0-rate,25,buff);
	fprintf(fp,"%s\n",buff);
	
	fclose(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -