我有这段代码,不幸的是它对我不起作用。我收到此错误:
RuntimeWarning:在 sqrt z1 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2) 中遇到无效值
RuntimeWarning:在 sqrt z2 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2) * (-1) 中遇到无效值
点的位置是:[nan nan nan]
编辑,我有这个问题的解决方案:
z1 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2)
z2 = np.sqrt(r1 ** 2 - x ** 2 - y ** 2) * (-1)
它不起作用,因为我们无法计算 NaN * (-1),这就是为什么其余代码不起作用的原因。
相反,我们应该像这样编写代码:
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)
现在它的工作正常。