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?