Monte Carlo Simulation(MCS)的使用

MCS估计二维联合概率密度离散值

考虑如下非线性动力系统:

动力系统 [1]

  • 参数值为:
    m , c , k 1 , k 3 , D = 1 , 0.5 , 1.5 , 2.5 , 1 / 2 m, c, k1, k3, D = 1, 0.5, 1.5, 2.5, 1/2 m,c,k1,k3,D=1,0.5,1.5,2.5,1/2

Python 代码如下:

代码中不需要导入以下所有依赖库,因方便,于是复制粘贴所有常用库。

import time
import sympy
import numpy as np
import pandas as pd
import seaborn as sns
import scipy
from scipy import stats
import matplotlib as mpl
from matplotlib import cm
import matplotlib.pyplot as plt
from scipy.special import erfc
from scipy.integrate import quad
from scipy.optimize import root,fsolve
from mpl_toolkits.mplot3d import Axes3D
from multiprocessing.pool import ThreadPool
from matplotlib.font_manager import FontProperties
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from scipy import special
from pysindy.feature_library import PolynomialLibrary,FourierLibrary
import torch

import warnings
warnings.filterwarnings('ignore')

mpl.rcParams['font.sans-serif'] = 'Times New Roman'
mpl.rcParams['axes.unicode_minus'] = 'True'
mpl.rcParams['font.size'] = 15

plt.rcParams['font.sans-serif'] = 'Times New Roman'
plt.rcParams['axes.unicode_minus'] = 'True'
# 用tex公式的形式输入英文和公式,以显示Times New Roman字体
plt.rcParams['mathtext.fontset'] = 'stix'
plt.rcParams['figure.dpi'] = 600 #分辨率

def MotionEquation(t,y,Wt,c,k1,k3):
    dxdt = y[1]
    dydt = -(c*y[1]+k1*y[0]+k3*np.power(y[0],3)) + Wt
    return np.array([dxdt,dydt])

def RK45(m, c, k1, k3, D):
    delta_t, N = 0.05, 1000000
    t = np.arange(0,N*delta_t,delta_t)
#     rng = np.random.Generator(np.random.MT19937(24))
    rng = np.random.default_rng(24)
    GWN = np.sqrt(2*D/delta_t)*rng.standard_normal(N)
#     np.random.seed(13)
#     GWN = np.sqrt(2*D/delta_t)*np.random.randn(N)
#     path = r'E:\matlb\bin\05_2021YanWork\20230724-SystemReduction\RamdonNumber.mat'
#     GWN = scipy.io.loadmat(path)['Wt']
#     GWN = GWN.reshape(GWN.shape[1])

    y = np.zeros((2,N+1))
    y[:,0] = np.array([0.01,0.01])
    for i in range(N):
        rk1 = MotionEquation(
            t[i],y[:,i],GWN[i],c,k1,k3
        )
        rk2 = MotionEquation(
            t[i]+delta_t/2,y[:,i]+rk1*delta_t/2,GWN[i],c,k1,k3
        )
        rk3 = MotionEquation(
            t[i]+delta_t/2,y[:,i]+rk2*delta_t/2,GWN[i],c,k1,k3
        )
        rk4 = MotionEquation(
            t[i]+delta_t,  y[:,i]+rk3*delta_t,  GWN[i],c,k1,k3
        )
        y[:,i+1] = y[:,i] + (rk1+2*rk2+2*rk3+rk4)*delta_t/6
    return t,y

def BasisFunc(x,xdot,m, c, k1, k3, D):
#         m, c, k1, k3, D  = 1, 0.5, 1.5, 2.5, 0.5
    dictionary = np.vstack([
        np.power(x,0), np.power(x,2), np.power(x,4), x*xdot,
        m*k3/D*np.power(x,3)*xdot,m*c*k1*k3/np.power(D,2)*np.power(x,5)*xdot,
        m*c/D*np.power(xdot,2), m*c*k3/(k1*D)*np.power(x,2)*np.power(xdot,2)
    ])
    return dictionary.T

m, c, k1, k3, D = 1, 0.5, 1.5, 2.5, 1/2
t,y = RK45(m, c, k1, k3, D)
print(t.shape,y.shape)

x1,x1dot = y[0,1:],y[1,1:]
x1_x1dot = np.vstack([x1,x1dot])

delta_x,delta_xdot = 0.05, 0.05
MCS_x1, MCS_x2 = np.arange(-4,4,delta_x), np.arange(-4,4,delta_xdot)

frequency = np.zeros((MCS_x1.shape[0],MCS_x2.shape[0]))
for i1 in range(MCS_x1.shape[0]):
    for j1 in range(MCS_x2.shape[0]):
        i,j = MCS_x1[i1], MCS_x2[j1]
        idx = np.array(
            [(x1_x1dot[0,:]>i-delta_x), (x1_x1dot[0,:]<i+delta_x), 
             (x1_x1dot[1,:]>j-delta_xdot), (x1_x1dot[1,:]<j+delta_xdot)]
        )
        frequency[i1,j1] = np.count_nonzero(np.all(idx,axis=0))
        
pdf_x1_x1dot = frequency/(x1.shape[0]*delta_x*2*delta_xdot*2)
MCS_x1_, MCS_x2_ = np.meshgrid(MCS_x1, MCS_x2)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_surface(
    MCS_x1_,MCS_x2_,pdf_x1_x1dot,
    cmap='Spectral_r'
)
plt.show()
plt.close()

运行结果与论文对比

本文代码运行结果
论文结果图[1]
[1] Tian, YP et al. Stationary response probability density of nonlinear random
vibrating systems: a data-driven method. Nonlinear Dynamics (2020) 100:2337–2352

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梚枫_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值