首页 > 解决方案 > 使用二维随机游走数值解来求解方程。有方法,但没有执行。帖子中的详细信息

问题描述

本质上,我正在尝试使用随机游走数值解来解决拉帕尔斯算子。我的域是一个圆,边界条件是一些函数:f(phi) = cos(2phi)。本质上,我试图在我的(单位)圆的 2D 域内取一个点,随机走它直到它遇到圆的边缘,所以当 x^2 + y+2 = 1 时。然后我要取x和y坐标,找到theta,然后将theta插入我的函数以获得一个值。我将记录并存储此值,并将此过程重复“NumbRepeats”次。然后取这些值的平均值。这应该给出方程的近似解。我对物理本身并不那么关心,我在上面列出的过程中需要帮助。它可能不一定是正确的,但如果我能让程序做我认为正确的事情,那么我会很开心。谢谢您的帮助。我将在下面发布我的代码:

请注意,我的专长不是编码,因此如果难以理解,请致歉。任何帮助表示赞赏。

import matplotlib.pyplot as plt
import numpy as np
import random


#Python code for 2D random walk
 
# defining the number of steps
n = 100000
 
#creating two array for containing x and y coordinate
#of size equals to the number of size and filled up with 0's
x = np.zeros(n)
y = np.zeros(n)

NumbRepeats = 100

PotBoundVec = []
for j in range(NumbRepeats):
    PotBound = 0
    for i in range(1, n):
        val = random.randint(0, 1)
        if val == .25:
            x[i] = x[i - 1] + 1
            y[i] = y[i - 1]
        elif val == .5:
            x[i] = x[i - 1] - 1
            y[i] = y[i - 1]
        elif val == .75:
            x[i] = x[i - 1]
            y[i] = y[i - 1] + 1
        else:
            x[i] = x[i - 1]
            y[i] = y[i - 1] - 1

    if x[i]**2 + y[i]**2 == 1:
        Theta = np.tan(x[i]/y[i])
        PotBound = np.cos(2*Theta)
        PotBoundVec.append(PotBound)
        x = np.zeros(n)
        y = np.zeros(n)
    else:
        pass

print(PotBoundVec)

标签: pythonrandom-walk

解决方案


推荐阅读