數學建模python matlab 編程(隨機游走問題)


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('位置')

 

老師的代碼:

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代碼:

 

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()

 

 

 

 

(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)

 

我的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')

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM