zoukankan      html  css  js  c++  java
  • 数学建模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')

  • 相关阅读:
    ubuntu上安装nginx+mysql+php5-fpm(PHP5
    创建内存盘
    ubuntu上安装apache2+mysql+php5-fpm(PHP5
    Cubieboard Linaro 搭建超节能监控平台
    CubieBoard开发板数据源介绍
    如何在github创建个人主页?
    android studio配置git
    greenDao使用时遇到的坑
    FragmentManager is already executing transactions
    AndroidSchedulers.mainThread()无法切换到主线程,原来是细节问题啊
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270719.html
Copyright © 2011-2022 走看看