Python 混沌台球仿真
我来寻求数学和编程方面的帮助 我在尝试做什么?我在尝试按照算法实现a的模拟 我是如何尝试的?使用numpy和matplotlib,我实现了以下代码Python 混沌台球仿真,python,math,numpy,physics,Python,Math,Numpy,Physics,我来寻求数学和编程方面的帮助 我在尝试做什么?我在尝试按照算法实现a的模拟 我是如何尝试的?使用numpy和matplotlib,我实现了以下代码 def boundaryFunction(parameter): return 1 + 0.1 * np.cos(parameter) def boundaryDerivative(parameter): return -0.1 * np.sin(parameter) def trajectoryFunction(parameter):
def boundaryFunction(parameter):
return 1 + 0.1 * np.cos(parameter)
def boundaryDerivative(parameter):
return -0.1 * np.sin(parameter)
def trajectoryFunction(parameter):
aux = np.sin(beta - phi) / np.sin(beta - parameter)
return boundaryFunction(phi) * aux
def difference(parameter):
return trajectoryFunction(parameter) - boundaryFunction(parameter)
def integrand(parameter):
rr = boundaryFunction(parameter)
dd = boundaryDerivative (parameter)
return np.sqrt(rr ** 2 + dd ** 2)
##### Main #####
length_vals = np.array([], dtype=np.float64)
alpha_vals = np.array([], dtype=np.float64)
# nof initial phi angles, alpha angles, and nof collisions for each.
n_phi, n_alpha, n_cols, count = 10, 10, 10, 0
# Length of the boundary
total_length, err = integrate.quad(integrand, 0, 2 * np.pi)
for phi in np.linspace(0, 2 * np.pi, n_phi):
for alpha in np.linspace(0, 2 * np.pi, n_alpha):
for n in np.arange(1, n_cols):
nu = np.arctan(boundaryFunction(phi) / boundaryDerivative(phi))
beta = np.pi + phi + alpha - nu
# Determines next impact coordinate.
bnds = (0, 2 * np.pi)
phi_new = optimize.minimize_scalar(difference, bounds=bnds, method='bounded').x
nu_new = np.arctan(boundaryFunction(phi_new) / boundaryDerivative(phi_new))
# Reflection angle with relation to tangent.
alpha_new = phi_new - phi + nu - nu_new - alpha
# Arc length for current phi value.
arc_length, err = integrate.quad(integrand, 0, phi_new)
# Append values to list
length_vals = np.append(length_vals, arc_length / total_length)
alpha_vals = np.append(alpha_vals, alpha)
count += 1
print "{}%" .format(100 * count / (n_phi * n_alpha))
问题是什么?当计算phi_new时,方程有两个解(假设边界是凸的),我必须强调,phi_new是不同于phi的解,但我不知道怎么做。代码还有其他问题吗
输出应该是什么?S x Alpha的相空间图
非常感谢您的帮助!提前感谢。您可以尝试的一种方法是(考虑到实际上只有两种解决方案),即
epsilon=1e-7#调整这个
δ=1e-4#调整此
# ...
bnds=(0,2*np.pi)
phi_new=optimize.minimize_标量(差,界限=bnds,方法='bounded').x
如果abs(phi_new-phi)
或者,您可以引入一个惩罚项,例如,
delta*exp(eps/(x-phi)^2)
,适当选择epsilon和delta。这是一个很好的解决方案。非常感谢。不幸的是,它还没有解决问题。肯定还有其他错误,尽管我确实使用了这本书摘录的算法。您是否看到任何其他可能的问题?是否可以0。检查不同的ε和δ值,1。张贴完整的代码,2。告诉我你期望看到的与你看到的相比,3。请输出phi_new、phi_new_1和phi_new_2,以确定您是否正确选择了epsilon和delta
epsilon = 1e-7 # tune this
delta = 1e-4 # tune this
# ...
bnds = (0, 2 * np.pi)
phi_new = optimize.minimize_scalar(difference, bounds=bnds, method='bounded').x
if abs(phi_new - phi) < epsilon:
bnds_1 = (0, phi - delta)
phi_new_1 = optimize.minimize_scalar(difference, bounds=bnds_1, method='bounded').x
bnds_2 = (phi + delta, 2 * np.pi)
phi_new_2 = optimize.minimize_scalar(difference, bounds=bnds_2, method='bounded').x
if difference(phi_new_1) < difference(phi_new_2):
phi_new = phi_new_1
else:
phi_new = phi_new_2