首页 技术 正文
技术 2022年11月16日
0 收藏 713 点赞 2,789 浏览 2409 个字

1

(1). 随机游走问题。在-1010的一维线段上,质点以1/5的概率用左运动1单位,以2/5的概率停止不动,以2/5的概率向右运动2单位,且碰到-10时向右运动3单位,碰到10时向左运动4单位。请画出它的轨迹。

我原来的代码matlab:

s=0;
now=0;
for i=1:400
x=unifrnd(0,1);
if x<=0.2
if now-1 <= -10
now=now-1+3;
else
now=now-1;
end
elseif x>0.6
if now+1 >= 10
now =now+1-3;
else
now=now+1;
end
end
s = [s now];
end
s
plot(s)
xlabel('时间')
ylabel('位置')

数学建模python matlab 编程(随机游走问题)

老师的代码:

Ctrl+R 注释 

Ctrl+T 取消注释

…… 换行不影响代码完整性

F5运行

clear
s(1)=0;t=300
for n=1:t
a=rand(1);
s(n+1)=(s(n)-1).*(a<1/5)+s(n).*(a<3/5&a>1/5)+......
(s(n)+2).*(a>3/5)
s(n+1)=-7.*(s(n+1)<=-10)+6.*(s(n+1)>=10)+s(n+1).*(s(n+1)<10&s(n+1)>-10);
end
plot(s,'O')

数学建模python matlab 编程(随机游走问题)

插入图例,标签,标题等

数学建模python matlab 编程(随机游走问题)

python代码:

import random
import matplotlib.pyplot as plt
s=[0]
for i in range(400):
a = random.random()
s.append((s[-1]-1)*(a<1/5)+s[-1]*(a<3/5 and a>1/5)+(s[-1]+2)*(a>3/5))
s[-1]=-7*(s[-1]<=-10)+6*(s[-1]>=10)+s[-1]*(s[-1]<10 and s[-1]>-10);
#plt.plot(s) fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = range(len(s))
#plt.scatter(y,s, marker='o')
ax.scatter(x,s,color='', marker='o', edgecolors='g',label='点的位置') #颜色为空,设置为空#解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False# 设置标题
ax.set_title('随机游走问题')
# 设置横坐标名称
ax.set_xlabel('时间')
# 设置纵坐标名称
ax.set_ylabel('位置')# 添加图例
#ax.legend(loc="upper left")
ax.legend(loc="") #自动选择一个空的位置
fig.show()

数学建模python matlab 编程(随机游走问题)

(2)考虑二维空间的布朗运动。

画图时加上横坐标和纵坐标说明,且考虑如何将图片插入到word文档中去。

考虑如何将图片中加上一横线和一纵线。

我的python代码:

import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
s=[0]
sy=[0]
for i in range(400):
x = random.random()
if x<=0.2:
if s[-1]-1 <= -10:
s.append(s[-1]-1+3)
else:
s.append(s[-1]-1)
elif x>0.2 and x<=0.6:
s.append(s[-1])
elif x>0.6:
if s[-1]+1 >= 10:
s.append(s[-1]+1-3)
else:
s.append(s[-1]+1) x = random.random()
if x<=0.2:
if sy[-1]-1 <= -10:
sy.append(sy[-1]-1+3)
else:
sy.append(sy[-1]-1)
elif x>0.2 and x<=0.6:
sy.append(sy[-1])
elif x>0.6:
if sy[-1]+1 >= 10:
sy.append(sy[-1]+1-3)
else:
sy.append(sy[-1]+1)fig = plt.figure()ax = fig.add_subplot(1,2,1)
ax.plot(s)
ax.set_ylabel('时间',color='r')
ax.set_xlabel('位置',color='c')ay = fig.add_subplot(1,2,2)
ay.plot(sy)
ay.set_ylabel('时间',color='r')
ay.set_xlabel('位置',color='c')fig.show()# 此处fig是二维
fig = plt.figure()
# 将二维转化为三维
axes3d = Axes3D(fig)
# axes3d.scatter3D(x,y,z)
# 效果相同
z=range(401)
axes3d.plot(s,sy,z)

数学建模python matlab 编程(随机游走问题)

我的matlab代码:

clear
s(1)=0;t=500
y(1)=0;
for n=1:t
a=rand(1);
s(n+1)=(s(n)-1).*(a<1/5)+s(n).*(a<3/5&a>1/5)+(s(n)+2).*(a>3/5)
s(n+1)=-7.*(s(n+1)<=-10)+6.*(s(n+1)>=10)+s(n+1).*(s(n+1)<10&s(n+1)>-10); a=rand(1);
y(n+1)=(y(n)-1).*(a<1/5)+y(n).*(a<3/5&a>1/5)+(y(n)+2).*(a>3/5)
y(n+1)=-7.*(y(n+1)<=-10)+6.*(y(n+1)>=10)+y(n+1).*(y(n+1)<10&y(n+1)>-10);
end
plot(s,y,'O')

数学建模python matlab 编程(随机游走问题)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,474
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,889
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,724
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,479
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,118
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,278