0

I have this code and unfortunately it doesn't work for me. I get this error:

RuntimeWarning: invalid value encountered in sqrt z1 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2)

RuntimeWarning: invalid value encountered in sqrt z2 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2) * (-1)

The location of the point is: [nan nan nan]

Edit, I have solution of this problem:

    z1 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2)
    z2 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2) * (-1) 

It is not working beacouse we can't calculate NaN * (-1) and thats why the rest of the code is not working.

Instead, we should write the code like this:

import cmath

    z1 = cmath.sqrt(pow(r1, 2.) - pow(x, 2.) - pow(y, 2.))
    zx = z1.real
    zy = z1.imag
    val = zx + zy
    z2 = val * (-1)

and now its working correct.

MarekOS
  • 33
  • 4
  • There is an error in your equations. At the z1, z2 lines x and y are both larger than r1 (for my made-up test input), making ```r1**2 - x**2 - y**2``` negative. – AbbeGijly Dec 10 '21 at 15:22
  • consider printing all the intermediate variables to make sure they're reasonable – AbbeGijly Dec 10 '21 at 15:23

0 Answers0