RuntimeWarning:在除法中遇到无效的值

问题:RuntimeWarning:在除法中遇到无效的值

我必须使用欧拉方法为“弹簧中的球”模型编写程序

from pylab import*
from math import*
m=0.1
Lo=1
tt=30
k=200
t=20
g=9.81
dt=0.01
n=int((ceil(t/dt)))
km=k/m
r0=[-5,5*sqrt(3)]
v0=[-5,5*sqrt(3)]
a=zeros((n,2))
r=zeros((n,2))
v=zeros((n,2))
t=zeros((n,2))
r[1,:]=r0
v[1,:]=v0
for i in range(n-1):
    rr=dot(r[i,:],r[i,:])**0.5
    a=-g+km*cos(tt)*(rr-L0)*r[i,:]/rr
    v[i+1,:]=v[i,:]+a*dt
    r[i+1,:]=r[i,:]+v[i+1,:]*dt
    t[i+1]=t[i]+dt

    #print norm(r[i,:])

plot(r[:,0],r[:,1])
xlim(-100,100)
ylim(-100,100)
xlabel('x [m]')
ylabel('y [m]')

show()

我不断收到此错误:

a=-g+km*cos(tt)*(rr-L0)*r[i,:]/rr
RuntimeWarning: invalid value encountered in divide

我无法弄清楚,代码有什么问题?

I have to make a program using Euler’s method for the “ball in a spring” model

from pylab import*
from math import*
m=0.1
Lo=1
tt=30
k=200
t=20
g=9.81
dt=0.01
n=int((ceil(t/dt)))
km=k/m
r0=[-5,5*sqrt(3)]
v0=[-5,5*sqrt(3)]
a=zeros((n,2))
r=zeros((n,2))
v=zeros((n,2))
t=zeros((n,2))
r[1,:]=r0
v[1,:]=v0
for i in range(n-1):
    rr=dot(r[i,:],r[i,:])**0.5
    a=-g+km*cos(tt)*(rr-L0)*r[i,:]/rr
    v[i+1,:]=v[i,:]+a*dt
    r[i+1,:]=r[i,:]+v[i+1,:]*dt
    t[i+1]=t[i]+dt

    #print norm(r[i,:])

plot(r[:,0],r[:,1])
xlim(-100,100)
ylim(-100,100)
xlabel('x [m]')
ylabel('y [m]')

show()

I keep getting this error:

a=-g+km*cos(tt)*(rr-L0)*r[i,:]/rr
RuntimeWarning: invalid value encountered in divide

I can’t figure it out, what is wrong with the code?


回答 0

我认为您的代码正在尝试“除以零”或“除以NaN”。如果您知道并且不想让它困扰您,那么您可以尝试:

import numpy as np
np.seterr(divide='ignore', invalid='ignore')

有关更多详细信息,请参见:

I think your code is trying to “divide by zero” or “divide by NaN”. If you are aware of that and don’t want it to bother you, then you can try:

import numpy as np
np.seterr(divide='ignore', invalid='ignore')

For more details see:


回答 1

Python索引从0(而不是1)开始,因此您的赋值“ r [1 ,:] = r0”定义r的第二个(即索引1)元素,而第一个(索引0)元素保留为一对零。for循环中i的第一个值为0,因此rr获取r中第一个条目与自身的点积的平方根(即0),并且在下一行中用rr除以引发错误。

Python indexing starts at 0 (rather than 1), so your assignment “r[1,:] = r0” defines the second (i.e. index 1) element of r and leaves the first (index 0) element as a pair of zeros. The first value of i in your for loop is 0, so rr gets the square root of the dot product of the first entry in r with itself (which is 0), and the division by rr in the subsequent line throws the error.


回答 2

为了防止被零除,您可以在div0错误发生的地方预初始化输出“ out”,例如np.where,由于不考虑条件而对整行进行了评估,因此不会将其切掉。

预初始化的示例:

a = np.arange(10).reshape(2,5)
a[1,3] = 0
print(a)    #[[0 1 2 3 4], [5 6 7 0 9]]
a[0]/a[1]   # errors at 3/0
out = np.ones( (5) )  #preinit
np.divide(a[0],a[1], out=out, where=a[1]!=0) #only divide nonzeros else 1

To prevent division by zero you could pre-initialize the output ‘out’ where the div0 error happens, eg np.where does not cut it since the complete line is evaluated regardless of condition.

example with pre-initialization:

a = np.arange(10).reshape(2,5)
a[1,3] = 0
print(a)    #[[0 1 2 3 4], [5 6 7 0 9]]
a[0]/a[1]   # errors at 3/0
out = np.ones( (5) )  #preinit
np.divide(a[0],a[1], out=out, where=a[1]!=0) #only divide nonzeros else 1

回答 3

您正在除以rr0.0。检查是否rr为零,并做一些合理的事情,而不是在分母中使用它。

You are dividing by rr which may be 0.0. Check if rr is zero and do something reasonable other than using it in the denominator.