Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting an error <string>:149: RuntimeWarning: invalid value encountered in sqrt while generating a list

Tags:

python

numpy

sqrt

def ellipse(numPoints, genX=np.linspace, HALF_WIDTH=10, HALF_HEIGHT=6.5):
    xs = 10.*genX(-1,1,numPoints)
    ys = 6.5*np.sqrt(1-(xs**2))
    return(xs, ys, "-")

I am getting an error that states that an invalid value was encountered in a squareroot. I can't see what it is.

sqrt(0) = 0
6.5*sqrt(1- (-1**2)) = 0

They should work, but the y values are having problems, they are returning "nan"

like image 694
Bob Unger Avatar asked Apr 08 '14 23:04

Bob Unger


People also ask

What is Runtimewarning invalid value encountered in double_scalars?

Why Is the Runtimewarning: Invalid Value Encountered in double_scalars Error Happening? The runtimewarning: invalid value encountered in double_scalars error happens when web developers try to perform certain mathematical operations that include numbers at the opposite end of the spectrum.

How do you take the square root of an array in Python?

sqrt() in Python. numpy. sqrt(array[, out]) function is used to determine the positive square-root of an array, element-wise.


1 Answers

probably xs**2 returns a number > 1 sqrt with negative number will return nan (not a number)

>>> import numpy as np
>>> np.sqrt(-1)
nan

If i am right numpy provides complex numbers functionality which i think is the only way to represent sqrt(x) where x<0

like image 98
Foo Bar User Avatar answered Sep 23 '22 04:09

Foo Bar User