37049

Numpy divide by zero. Why?

import pygame import random import math import numpy as np import matplotlib.pyplot as plt fx = np.zeros([11]) fy = np.zeros([11]) x = np.zeros([11]) y = np.zeros([11]) x[0] = 11 y[0] = 1 sigma = 1.01 e = 1.1 dt = 0.1 def LJ(x,y): for i in range(1,10): for j in range(1,10): rx = (x[i]-x[j]) ry = (y[i]-y[j]) fx[i] = 24*e*(((2/rx)*sigma/rx**12)-((1/rx)*sigma/rx**6)) fy[i] = 24*e*(((2/ry)*sigma/ry**12)-((1/ry)*sigma/ry**6)) print fx, fy

Why am I still getting errors RuntimeWarning: divide by zero encountered in double_scalars and

RuntimeWarning: invalid value encountered in double_scalars

The result I am getting is

[ 0. nan nan nan nan nan nan nan nan nan 0.] [ 0. nan nan nan nan nan nan nan nan nan 0.]

I tried to modify starting x and y but that gave no effect.

Answer1:

In this code:

def LJ(x,y): for i in range(1,10): for j in range(1,10): ...

If i == j, you are comparing a particle to itself. Try skipping that iteration of the for-loops like so:

def LJ(x,y): for i in range(1,10): for j in range(1,10): if i == j: continue rx = (x[i]-x[j]) ry = (y[i]-y[j]) fx[i] = 24*e*(((2/rx)*sigma/rx**12)-((1/rx)*sigma/rx**6)) fy[i] = 24*e*(((2/ry)*sigma/ry**12)-((1/ry)*sigma/ry**6))

Also, you will need to put in actual values for the x and y lists, as they are all currently 0's. Two particles at exactly the same location exert an infinite amount of force according to that equation, so the divide by 0 is accurate in that scenario.

Answer2:

You should stay with the physics. The potential function is, reconstructing from your force formula,

LJ(r) = 4*e*( (sigma/r)**12 - (sigma/r)**6 )

The force corresponding to the potential is, for any potential,

F = - LJ'(r)*(rx/r, ry/r)

Thus the procedure to compute the combined interaction forces should look like

def LJ(x,y): for i in range(x.size): for j in range(x.size): rx = (x[i]-x[j]) ry = (y[i]-y[j]) r = sqrt(rx*rx+ry*ry+1e-12) f = 24*e/r*( 2*(sigma/r)**12 - (sigma/r)**6 ) fx[i] += f*(rx/r) fy[i] += f*(ry/r) print fx, fy

The condition is that at the call of the procedure the force array is initialized, either to zero or the gravitational forces only.

<hr>

The addition of the term eps**2 in the computation of the distance serves to avoid zero distances and thus infinite or very large values in the force computation. This epsilon eps should of course be small against typical distances of particles resp. the dimension of the scene, here sigma=1.01 is a typical distance, thus eps=1e-6 is small against it.

Recommend

  • Error - Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs)= etc
  • What could cause numpy.nanstd() to return nan?
  • Does Angular assign itself to `window.angular` globally, when loaded as CommonJS module?
  • Where to get the .java files of a netbeans project?
  • When querying against a view, a filtering clause in the view's definition is being ignored
  • Handling exceptions in a class library enveloping a device driver
  • Play Framework nested form errors missing
  • Is C++ compilable with OpenMP and boost on MacOS?
  • Where I store the custom exceptions in cakephp 3?
  • How to get Apache XML-RPC 3.1.3 compliance (ISO date format along with time zone) in Java 1.6
  • Rails 5 - Google Maps - Javascript error - initMap is not a function - fixing one js issue creates a
  • maven jboss-as:start A required class was missing … org/sonaty…/ArtifactResolutionException
  • d3.js: why is d3.geo.path() giving NaN?
  • Generic/Unknown HTTP Error with response code 0 using UnityWebRequest
  • Vigenere cipher not working
  • WordPress > setting permalink option via script buggy?
  • Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • Is it possible to define rest argument in OCaml?
  • How do I get the list of bad records that didn't load in Bigquery?
  • TFS 2015 - Waiting for an agent to be requested
  • Elasticsearch script query involving root and nested values
  • Symfony 2. CSRF token is invalid
  • ASP.NET MVC Application won't update some controllers
  • Web.config system.webserver errors
  • CakePHP ACL tutorial initDB function warnings
  • How to revert to previous XCode version?
  • java inputstream
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Using variable in a value field in jMeter
  • Where to put my custom functions in Wordpress?
  • Apache 2.4 - remove | delete | uninstall
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Converting MP3 duration time