Huo Lee
Huo Lee

Reputation: 9

Why am I getting float division by zero?

I want to find the potential value and optimize it using the minimize function but when I run the codes, I get an error saying float division by zero. Does anyone know what's the problem?

Below are my codes.

def LennardJones(r):

"""
return Potential at input x for a polynomial with input r
"""
Potential = 0
for i in range(len(r)):
    Potential += 4 * 0.41 * ((2.4 / i)**12 - (2.4 / i)**6)
return Potential

Answer = minimize(LennardJones, 1)

Upvotes: 0

Views: 350

Answers (4)

Kiran Siddeshwar
Kiran Siddeshwar

Reputation: 11

I think the 'i' value begins with 0 when you don't specify the range explicitly. You can use range(1, len(r)), and check if the problem still persists! Check the documentation for more details.

Upvotes: 1

Ali Tahir
Ali Tahir

Reputation: 379

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default)

At the start, i is 0, and division by zero is not possible.

however, it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6)

Upvotes: 1

Andrei Berenda
Andrei Berenda

Reputation: 1986

for i in range(len(r)):

for i in range(...) usually starts with 0.

Upvotes: 1

Adi OS
Adi OS

Reputation: 162

Maybe you should check why not add a parameter in method LennardJones.

Upvotes: 0

Related Questions