英文:
Really weird issue for a simple calculation with Python code
问题
我有以下两个函数用于进行一些简单的物理计算。
def force(r):
if r == 0:
return 0
else:
return 1/r**13 - 1/r**7
def calculate_forces_and_positions(x, l):
n = len(x) # 当前点的数量
forces = np.zeros_like(x)
# 计算力
for i in range(n):
left_neighbour = (x[i] - x[i-1]) / l[i] if i > 0 else x[i] / l[i]
right_neighbour = (x[i+1] - x[i]) / l[i] if i < n - 1 else 0
forces[i] = -force(right_neighbour) + force(left_neighbour)
print(f"n= {i} and L ={left_neighbour} R = {right_neighbour} Force{i} = {forces[i]} force-R = {force(right_neighbour)} force-L= {force(left_neighbour)}")
# 计算位置
print(forces)
x_new = x + dt * forces
return forces, x_new
然后我传入 x[1,2,3] 和 l[1,1,1]。令人惊讶的是,我得到以下输出:
n= 0 and L =0.9900990099009901 R = 0.9900990099009901 Force0 = 0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 1 and L =0.9900990099009901 R = 0.9900990099009901 Force1 = 0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 2 and L =0.9900990099009901 R = 0 Force2 = 0 force-R = 0 force-L= 0.06595792832627945
所以对于 n = 2,Force2 = 0,但明显应该是 force-R + force-L = 0.06。我不知道问题出在哪里,感到非常困惑。我该如何修复它?
英文:
I have the following two functions to do some simple physics calculation.
def force(r):
if r == 0:
return 0
else:
return 1/r**13 - 1/r**7
def calculate_forces_and_positions(x, l):
n = len(x) # Current number of points
forces = np.zeros_like(x)
# Calculate forces
for i in range(n):
left_neighbour = (x[i] - x[i-1]) / l[i] if i > 0 else x[i] / l[i]
right_neighbour = (x[i+1] - x[i]) / l[i] if i < n - 1 else 0
forces[i] = -force(right_neighbour) + force(left_neighbour)
print(f"n= {i} and L ={left_neighbour} R = {right_neighbour} Force{i} = {forces[i]} force-R = {force(right_neighbour)} force-L= {force(left_neighbour)}")
# Calculate positions
print(forces)
x_new = x + dt * forces
return forces, x_new
Then I pass x[1,2,3] and l[1,1,1]. Surprisingly, I get this output:
n= 0 and L =0.9900990099009901 R = 0.9900990099009901 Force0 = 0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 1 and L =0.9900990099009901 R = 0.9900990099009901 Force1 = 0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 2 and L =0.9900990099009901 R = 0 Force2 = 0 force-R = 0 force-L= 0.06595792832627945
So for n = 2 Force2 = 0, but it is clearly force-R + force-L = 0.06. I don't know where things go wrong and I am really confused. How can I fix it?
答案1
得分: 3
np.zeros_like
创建了一个int64数组。在你的示例中,当你将0.06转换为整数时,得到的是零。如果你想要浮点数,需要指定dtype
。
forces = np.zeros_like(x, dtype=float)
输出:
n= 0 和 L =0.9900990099009901 R = 0.9900990099009901 Force0 = 0.0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 1 和 L =0.9900990099009901 R = 0.9900990099009901 Force1 = 0.0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 2 和 L =0.9900990099009901 R = 0 Force2 = 0.06595792832627945 force-R = 0 force-L= 0.06595792832627945
另外,作为一项附注,你没有传递l[1,1,1]
,而是传递了[1.01,1.01,1.01]
。
英文:
np.zeros_like
is creating an int64 array. In your example, when you convert 0.06 to an integer, you get zero. You need to specify the dtype
if you want floats.
forces = np.zeros_like(x, dtype=float)
Output:
n= 0 and L =0.9900990099009901 R = 0.9900990099009901 Force0 = 0.0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 1 and L =0.9900990099009901 R = 0.9900990099009901 Force1 = 0.0 force-R = 0.06595792832627945 force-L= 0.06595792832627945
n= 2 and L =0.9900990099009901 R = 0 Force2 = 0.06595792832627945 force-R = 0 force-L= 0.06595792832627945
And, as a side note, you are not passing l[1,1,1]. You are passing [1.01,1.01,1.01].
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论