Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))
like image 459
Ali R. Avatar asked Feb 10 '16 15:02

Ali R.


2 Answers

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
like image 132
Ami Tavory Avatar answered Nov 19 '22 05:11

Ami Tavory


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

like image 34
ramblinknight Avatar answered Nov 19 '22 04:11

ramblinknight