数学建模 of python(计算机仿真)

吉吉:

       最近在学习数学建模,但是matlab用的不是很习惯,于是我尝试用python解决几道,别说还蛮顺手,以下知识点是老师ppt上面的,代码我改成python的,Ok正文开始:

首先随便看下定义(哈哈):计算机仿真是一种非实物仿真方法,是用计算机对一个系统的结构和行为进行动态演示, 以评价或预测一个系统的行为效果,为决策提供信息的一种方法.它是解决较复杂的实际问题的一条有效途径计算机仿真通过建立数学模型、编制计算机程序实现对真实系统的模拟,从而了解系统随时间变化的行为或特性。

是不是很抽象,抽象就对了,举个栗子:我们都知道三峡水库吧,号称世界最大的水电站(此处应有掌声!啪啪啪!)所以它的安全问题是一个很重要的问题,我们不可能等他建好在看看它的安全性吧,所以这时候就可以利用计算机来模拟,这就产生了计算机仿真这一领域。好啦有了初步了解,我们看两道题(懒得打字所以直接上图,见谅。。。):

题目很简单,代码更简单,直接上:

# -*- coding: utf-8 -*-
"""
Created on Sat Aug 11 20:17:43 2018

@author: jj
"""
'''
* ━━━━━━神兽出没━━━━━━
*    ┏┓   ┏┓
*   ┏┛┻━━━┛┻┓
*   ┃       ┃
*   ┃   ━   ┃
*   ┃ ┳┛ ┗┳ ┃
*   ┃       ┃
*   ┃   ┻   ┃
*   ┃       ┃
*   ┗━┓   ┏━┛Code is far away from bug with the animal protecting
*     ┃   ┃ 神兽保佑,代码无bug
*     ┃   ┃
*     ┃   ┗━━━┓
*     ┃       ┣┓
*     ┃       ┏┛
*     ┗┓┓┏━┳┓┏┛
*      ┃┫┫ ┃┫┫
*      ┗┻┛ ┗┻┛
*
* ━━━━━━感觉萌萌哒━━━━━━
'''
import random
import numpy as np
#开车时间的仿真测试
s1 = 0;s3 = 0
for i in range(10000):
    s = random.random()
    if s<0.7:
        s1+=1
    elif s>0.9:
        s3+=1
print(s1/10000,1-s1/10000-s3/10000,s3/10000)
#人到达时刻仿真测试
s1,s2,s3,s4=0,0,0,0
for i in range(10000):
    s = random.random()
    if s<0.3:
        s1+=1
    elif s<0.7:
        s2+=1
    elif s<0.9:
        s3+=1
    else:
        s4+=1
print(s1/10000,s2/10000,s3/10000,s4/10000)
#火车运行时间仿真测试
s = np.random.normal(0,1,10000)
y = []
for i in range(10000):
    y[i] = 2*s[i]+30
    
#赶上火车的仿真结果
x1 = [random.random() for i in range(10000)]
x2 = [random.random() for i in range(10000)]
x3 = np.random.normal(0,1,10000)
s = 0
for i in range(10000):
    if x1[i]<0.7:
        T1=0
    elif x1[i]<0.9:
        T1=5
    else:T1=10
    
    T2=30+2*x3[i]
    if x2[i]<0.3:
        T3=28
    elif x2[i]<0.7:
        T3=30
    elif x2[i]<0.9:
        T3=32
    else:T3=34
#    print(T3)
    if (T3<T2+T1).all():
        s+=1
    continue
print(s/10000)

看第二题:

代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 12 18:49:26 2018

@author: jj
"""
import random
'''
* ━━━━━━神兽出没━━━━━━
*    ┏┓   ┏┓
*   ┏┛┻━━━┛┻┓
*   ┃       ┃
*   ┃   ━   ┃
*   ┃ ┳┛ ┗┳ ┃
*   ┃       ┃
*   ┃   ┻   ┃
*   ┃       ┃
*   ┗━┓   ┏━┛Code is far away from bug with the animal protecting
*     ┃   ┃ 神兽保佑,代码无bug
*     ┃   ┃
*     ┃   ┗━━━┓
*     ┃       ┣┓
*     ┃       ┏┛
*     ┗┓┓┏━┳┓┏┛
*      ┃┫┫ ┃┫┫
*      ┗┻┛ ┗┻┛
*
* ━━━━━━感觉萌萌哒━━━━━━
'''
x1=[random.random() for i in range(365)]
x2=[random.random() for i in range(365)]
def a():
    for i in range(1,365):
        if x1[i]<0.35:
            if x2[i]<0.03 :
                news=40
            elif x2[i]<0.08:
                news=50
            elif x2[i]<0.23:
                news=60
            elif x2[i]<0.43:
                news=70
            elif x2[i]<0.78:
                news=80
            elif x2[i]<0.93:
                news=90
            else :
                news=100
        elif x1[i]<0.8:
            if x2[i]<0.10:
                news=40
            elif x2[i]<0.28:
                news=50
            elif x2[i]<0.68:
                news=60
            elif x2[i]<0.88:
                news=70
            elif x2[i]<0.96:
                news=80
            else :
                news=90
    
        else:
            if x2[i]<0.44:
                news=40
            elif x2[i]<0.66:
                news=50
            elif x2[i]<0.82:
                news=60
            elif x2[i]<0.94:
                news=70
            else :
                news=80
    return news


def sb(n,news):
    paper = 10*n
    if paper>=news:    #news为需求量
        sale=news
        remand=paper-news  #remand为需要退回的报纸        
    else:
        sale=paper
        remand=0
    sb = 2*sale-1.3*paper+0.2*remand#sb代表利润
    return sb
if __name__ == '__main__':
    news=a()
    optmoney=sb(4,news)
    for i in range(5,11):
        if sb(i,news)>optmoney:
           optnews=i*10
           optmoney=sb(i,news)
#        print(i,sb(i)/364)
        print(optnews,optmoney,optmoney/364)

好啦,这次就到这里,舒服。。。

猜你喜欢

转载自blog.csdn.net/weixin_41503009/article/details/81613044