Ali R.
Ali R.

Reputation: 443

RuntimeWarning: invalid value encountered in arccos

I am new to using Python but getting along with it fairly well. I keep getting the error you see below and not sure what the problem is exactly as I believe the values are correct and stated. What do you think the problem exactly is? I am trying to graph from t = 0 to t=PM, and the formula you see below is angle arccos.

Couldn't find the troubleshooting of this arccos error online. Running Python 3.5.

import numpy as np
import matplotlib
from matplotlib import pyplot
from __future__ import division

rE = 1.50*(10**11)
rM = 3.84*(10**8)
PE = 3.16*(10**7)
PM = 2.36*(10**6)

t = np.linspace(0, PM, 200)

# anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in arccos
y = 0.5*(np.arccos(2*(np.pi)*t*((1/PM)-(1/PE))+90))

Upvotes: 16

Views: 38329

Answers (2)

Ami Tavory
Ami Tavory

Reputation: 76297

If you simplify to just

np.arccos(90)

(which is the first element in the array being passed to arccos), you'll get the same warning

Why is that? arccos() attempts to solve x for which cos(x) = 90. However, such a value doesn't make sense as it's outside of the possible domain for arccos [-1,1]

Also note that at least in recent versions of numpy, this calculation returns nan

>>> import numpy as np
>>> b = np.arccos(90)
__main__:1: RuntimeWarning: invalid value encountered in arccos
>>> b
nan

Upvotes: 16

ramblinknight
ramblinknight

Reputation: 81

The np.arccos() function can only take values between -1 and 1, inclusive.

See: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.arccos.html

Upvotes: 8

Related Questions