【Matlab编程】马氏链随机模拟

2024-03-24 15:08

本文主要是介绍【Matlab编程】马氏链随机模拟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文是利用蒙特卡罗算法对马氏链过程的模拟。假设有10个状态,从每个状态到与之相邻状态的概率是相同的,仿真次数为1000,及进行了1000次状态转移。我们以动画的形式再现了状态转移的过程,并记录了到达每个状态的次数,具体实现如下:

close all;clc;clear; 
figure; 
s=1;
n=1000;
r=1; % 圆圈的半径
title('等概率情况的计算机模拟')
set(gcf,'doublebuffer','on'); % 设置图形渲染效果
xlabel('Please press "space" key and see the result!',... 'fontsize',14,'color','r'); % 添加标注文字
hold on;axis equal; % 设置坐标轴属性
axis([-16,16,-16,16]); % 设置坐标轴范围
fill(r*sin(0:.1:2*pi)-7,r*cos(0:.1:2*pi)+13,'w'); % 画出固定点P1
hold on
fill(r*sin(0:.1:2*pi)-11,r*cos(0:.1:2*pi)+9,'w');hold on % 画出固定点P2 
fill(r*sin(0:.1:2*pi)-3,r*cos(0:.1:2*pi)+9,'w'); hold on% 画出固定点P3
fill(r*sin(0:.1:2*pi)+5,r*cos(0:.1:2*pi)+9,'w');hold on % 画出固定点P4 
fill(r*sin(0:.1:2*pi)+9,r*cos(0:.1:2*pi)+5,'w');hold on % 画出固定点P5
fill(r*sin(0:.1:2*pi)-15,r*cos(0:.1:2*pi)-3,'w');hold on % 画出固定点P6
fill(r*sin(0:.1:2*pi)+1,r*cos(0:.1:2*pi)-3,'w'); hold on% 画出固定点P7
fill(r*sin(0:.1:2*pi)+13,r*cos(0:.1:2*pi)-3,'w');hold on % 画出固定点P8
fill(r*sin(0:.1:2*pi)-7,r*cos(0:.1:2*pi)-11,'w');hold on % 画出固定点P9
fill(r*sin(0:.1:2*pi)+5,r*cos(0:.1:2*pi)-15,'w');hold on % 画出固定点P10
text(-15.4,-3,'6','FontSize',18);hold on
text(-11.4,9,'2','FontSize',18);hold on
text(-7.4,13,'1','FontSize',18);hold on
text(-7.4,-11,'9','FontSize',18);hold on
text(-3.4,9,'3','FontSize',18);hold on
text(0.6,-3,'7','FontSize',18);hold on
text(4.6,9,'4','FontSize',18);hold on
text(4.1,-15,'10','FontSize',18);hold on
text(8.6,5,'5','FontSize',18);hold on
text(12.6,-3,'8','FontSize',18);hold on
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%plot([8.5,6],[6,8.3],'r-')
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x45=fliplr(8.3:-0.1:5.8);
y45=fliplr(linspace(5.9,8.2,length(x45)));
x54=8.3:-0.1:5.8;
y54=linspace(5.9,8.2,length(x54));
x85=fliplr(9.4:0.05:12.4);
y85=fliplr(linspace(4.1,-2.2,length(x85)));
x58=9.4:0.05:12.4;
y58=linspace(4.1,-2.2,length(x58));
x80=fliplr(5.8:0.1:12.2);
y80=fliplr(linspace(-14.4,-3.8,length(x80)));
x08=5.8:0.1:12.2;
y08=linspace(-14.4,-3.8,length(x08));
x87=fliplr(2:0.1:12);
y87=-3*ones(1,length(x87));
x78=2:0.1:12;
y78=-3*ones(1,length(x78));
x79=fliplr(-6.2:0.1:0.4);
y79=fliplr(linspace(-10.4,-3.8,length(x79)));
x97=-6.2:0.1:0.4;
y97=linspace(-10.4,-3.8,length(x97));
x73=fliplr(-2.6:0.06:0.9);
y73=fliplr(linspace(7.9,-2,length(x73)));
x37=-2.6:0.06:0.9;
y37=linspace(7.9,-2,length(x37));
x13=-6.4:0.1:-3.8;
y13=linspace(12.2,9.6,length(x13));
x31=fliplr(-6.4:0.1:-3.8);
y31=linspace(9.6,12.2,length(x31));
x67=-14:.11:0;
y67=-3*ones(1,length(x67));
x76=0:-0.11:-14;
y76=-3*ones(1,length(x76));
x21=-10.1:.1:-7.8;
y21=linspace(9.5,12.4,length(x21));
x12=-7.8:-0.1:-10.1;
y12=fliplr(linspace(9.5,12.4,length(x12)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t6=text(-15.3,-5,'0','FontSize',12,'Color',[1 0 0]);
t2=text(-11.4,7,'0','FontSize',12,'Color',[1 0 0]);
t1=text(-7.2,11,'0','FontSize',12,'Color',[1 0 0]);
t9=text(-7.3,-13,'0','FontSize',12,'Color',[1 0 0]);
t3=text(-4,7,'0','FontSize',12,'Color',[1 0 0]);
t7=text(0.6,-5,'0','FontSize',12,'Color',[1 0 0]);
t4=text(4.7,7,'0','FontSize',12,'Color',[1 0 0]);
t10=text(4.3,-13,'0','FontSize',12,'Color',[1 0 0]);
t5=text(8.3,3,'0','FontSize',12,'Color',[1 0 0]);
t8=text(12.6,-5,'0','FontSize',12,'Color',[1 0 0]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:length(x45)plot(x45(i),y45(i),'*')endfor i=1:length(x54)plot(x54(i),y54(i),'*')endfor i=1:length(x85)plot(x85(i),y85(i),'*')endfor i=1:length(x58)plot(x58(i),y58(i),'*')endfor i=1:length(x80)plot(x80(i),y80(i),'.')endfor i=1:length(x08)plot(x08(i),y08(i),'*')
endfor i=1:length(x87)plot(x87(i),y87(i),'*')endfor i=1:length(x78)plot(x78(i),y78(i),'*')
endfor i=1:length(x79)plot(x79(i),y79(i),'.')endfor i=1:length(x97)plot(x97(i),y97(i),'*')endfor i=1:length(x73)plot(x73(i),y73(i),'*')endfor i=1:length(x37)plot(x37(i),y37(i),'.')endfor i=1:length(x31)plot(x31(i),y31(i),'*')
endfor i=1:length(x13)plot(x13(i),y13(i),'*')endfor i=1:length(x67)plot(x67(i),y67(i),'*')endfor i=1:length(x76)plot(x76(i),y76(i),'*')endfor i=1:length(x21)plot(x21(i),y21(i),'*')endfor i=1:length(x12)plot(x12(i),y12(i),'*')end
plot(x45,y45,'w.')
plot(x85,y85,'w.')
plot(x80,y80,'w.')
plot(x87,y87,'w.')
plot(x79,y79,'w.')
plot(x73,y73,'w.')
plot(x31,y31,'w.')
plot(x21,y21,'w.')
plot(x67,y67,'w.')plot(x54,y54,'w.')
plot(x58,y58,'w.')
plot(x08,y08,'w.')
plot(x78,y78,'w.')
plot(x97,y97,'w.')
plot(x37,y37,'w.')
plot(x13,y13,'w.')
plot(x12,y12,'w.')
plot(x76,y76,'w.')  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%s=1;p1=0;%pn为到达n村庄的次数p2=0;p3=0;p4=0;p5=0;p6=0;p7=0;p8=0;p9=0;p10=0;%plot([-14,0],[-3,-3],'b','linewidth',3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:nm=get(gcf,'currentkey'); % 获取键入按键的名称if strcmp(m,'space'); % 检查按下的按键是否为空格键break;end if s==1possible=round(rand(1));if possible==1s=3;p3=p3+1;for i=1:length(x13)plot(x13(i),y13(i),'.')pause(0.00000000001)enddelete(t3);t3=text(-4,7,num2str(p3),'FontSize',12,'Color',[1 0 0]);elses=2;p2=p2+1;for i=1:length(x12)plot(x12(i),y12(i),'.')pause(0.00000000001)enddelete(t2)t2=text(-11.4,7,num2str(p2),'FontSize',12,'Color',[1 0 0]);endelseif s==2s=1;p1=p1+1;for i=1:length(x21)plot(x21(i),y21(i),'.')pause(0.00000000001)enddelete(t1)t1=text(-7.2,11,num2str(p1),'FontSize',12,'Color',[1 0 0]);elseif s==3possible=round(rand(1));if possible==1s=7;p7=p7+1;for i=1:length(x37)plot(x37(i),y37(i),'.')pause(0.00000000001)enddelete(t7)t7=text(0.6,-5,num2str(p7),'FontSize',12,'Color',[1 0 0]);elses=1;p1=p1+1;for i=1:length(x31)plot(x31(i),y31(i),'.')pause(0.00000000001)enddelete(t1)t1=text(-7.2,11,num2str(p1),'FontSize',12,'Color',[1 0 0]);endelseif s==4s=5;p5=p5+1;for i=1:length(x45)plot(x45(i),y45(i),'.')pause(0.00000000001)enddelete(t5)t5=text(8.3,3,num2str(p5),'FontSize',12,'Color',[1 0 0]);elseif s==5possible=round(rand(1));if possible==1s=8;p8=p8+1;for i=1:length(x58)plot(x58(i),y58(i),'.')pause(0.00000000001)enddelete(t8)t8=text(12.6,-5,num2str(p8),'FontSize',12,'Color',[1 0 0]);elses=4;p4=p4+1;for i=1:length(x54)plot(x54(i),y54(i),'.')pause(0.00000000001)enddelete(t4)t4=text(4.7,7,num2str(p4),'FontSize',12,'Color',[1 0 0]);endelseif s==6s=7;p7=p7+1;for i=1:length(x67)plot(x67(i),y67(i),'.')pause(0.00000000001)enddelete(t7)t7=text(0.6,-5,num2str(p7),'FontSize',12,'Color',[1 0 0]);elseif s==7possible=floor(rand(1)*4);if possible==0s=6;p6=p6+1;for i=1:length(x76)plot(x76(i),y76(i),'.')pause(0.00000000001)enddelete(t6)t6=text(-15.3,-5,num2str(p6),'FontSize',12,'Color',[1 0 0]);elseif possible==1s=3;p3=p3+1;for i=1:length(x73)plot(x73(i),y73(i),'.')pause(0.00000000001)enddelete(t3)t3=text(-4,7,num2str(p3),'FontSize',12,'Color',[1 0 0]);elseif possible==2s=9;p9=p9+1;for i=1:length(x79)plot(x79(i),y79(i),'.')pause(0.00000000001)enddelete(t9)t9=text(-7.3,-13,num2str(p9),'FontSize',12,'Color',[1 0 0]);elses=8;p8=p8+1;for i=1:length(x78)plot(x78(i),y78(i),'.')pause(0.00000000001)enddelete(t8)t8=text(12.6,-5,num2str(p8),'FontSize',12,'Color',[1 0 0]);endelseif s==8possible=floor(rand(1)*3);if possible==0s=7;p7=p7+1;for i=1:length(x87)plot(x87(i),y87(i),'.')pause(0.00000000001)enddelete(t7)t7=text(0.6,-5,num2str(p7),'FontSize',12,'Color',[1 0 0]);elseif possible==1s=5;p5=p5+1;for i=1:length(x85)plot(x85(i),y85(i),'.')pause(0.00000000001)enddelete(t5)t5=text(8.3,3,num2str(p5),'FontSize',12,'Color',[1 0 0]);else s=10;p10=p10+1;for i=1:length(x80)plot(x80(i),y80(i),'.')pause(0.00000000001)enddelete(t10)t10=text(4.3,-13,num2str(p10),'FontSize',12,'Color',[1 0 0]);endelseif s==9s=7;p7=p7+1;for i=1:length(x97)plot(x97(i),y97(i),'.')pause(0.00000000001)enddelete(t7)t7=text(0.6,-5,num2str(p7),'FontSize',12,'Color',[1 0 0]);else s=8;p8=p8+1;for i=1:length(x08)plot(x08(i),y08(i),'.')pause(0.00000000001)enddelete(t8)t8=text(12.6,-5,num2str(p8),'FontSize',12,'Color',[1 0 0]);end
plot(x45,y45,'w.')
plot(x85,y85,'w.')
plot(x80,y80,'w.')
plot(x87,y87,'w.')
plot(x79,y79,'w.')
plot(x73,y73,'w.')
plot(x31,y31,'w.')
plot(x21,y21,'w.')
plot(x67,y67,'w.')
plot(x54,y54,'w.')
plot(x58,y58,'w.')
plot(x08,y08,'w.')
plot(x78,y78,'w.')
plot(x97,y97,'w.')
plot(x37,y37,'w.')
plot(x13,y13,'w.')
plot(x12,y12,'w.')
plot(x76,y76,'w.')  
endfor j=i:nif s==1possible=round(rand(1));if possible==1s=3;p3=p3+1;elses=2;p2=p2+1;endelseif s==2s=1;p1=p1+1;elseif s==3possible=round(rand(1));if possible==1s=7;p7=p7+1;elses=1;p1=p1+1;endelseif s==4s=5;p5=p5+1;elseif s==5possible=round(rand(1));if possible==1s=8;p8=p8+1;elses=4;p4=p4+1;endelseif s==6s=7;p7=p7+1;elseif s==7possible=floor(rand(1)*4);if possible==0s=6;p6=p6+1;elseif possible==1s=3;p3=p3+1;elseif possible==2s=9;p9=p9+1;elses=8;p8=p8+1;endelseif s==8possible=floor(rand(1)*3);if possible==0s=7;p7=p7+1;elseif possible==1s=5;p5=p5+1;else s=10;p10=p10+1;endelseif s==9s=7;p7=p7+1;else s=8;p8=p8+1;endend
delete(t1)
delete(t2)
delete(t3)
delete(t4)
delete(t5)
delete(t6)
delete(t7)
delete(t8)
delete(t9)
delete(t10)
t6=text(-15.3,-5,num2str(p6),'FontSize',12,'Color',[1 0 0]);
t2=text(-11.4,7,num2str(p2),'FontSize',12,'Color',[1 0 0]);
t1=text(-7.2,11,num2str(p1),'FontSize',12,'Color',[1 0 0]);
t9=text(-7.3,-13,num2str(p9),'FontSize',12,'Color',[1 0 0]);
t3=text(-4,7,num2str(p3),'FontSize',12,'Color',[1 0 0]);
t7=text(0.6,-5,num2str(p7),'FontSize',12,'Color',[1 0 0]);
t4=text(4.7,7,num2str(p4),'FontSize',12,'Color',[1 0 0]);
t10=text(4.3,-13,num2str(p10),'FontSize',12,'Color',[1 0 0]);
t5=text(8.3,3,num2str(p5),'FontSize',12,'Color',[1 0 0]);
t8=text(12.6,-5,num2str(p8),'FontSize',12,'Color',[1 0 0]);
仿真过程如下:

最终的结果如下:


原文:http://blog.csdn.net/tengweitw/article/details/34063833

作者:nineheadedbird



这篇关于【Matlab编程】马氏链随机模拟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/841962

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

hdu4431麻将模拟

给13张牌。问增加哪些牌可以胡牌。 胡牌有以下几种情况: 1、一个对子 + 4组 3个相同的牌或者顺子。 2、7个不同的对子。 3、13幺 贪心的思想: 对于某张牌>=3个,先减去3个相同,再组合顺子。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOExcepti

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

深入理解RxJava:响应式编程的现代方式

在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 文章目录 💯 什么是RxJava?💯 响应式编程的优势💯 RxJava的核心概念

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点