math.sqrt() ValueError: math domain error
math.sqrt() ValueError: math domain error
我正在尝试使用 matplotlib 和微积分绘制图形,但我的 f(x) 函数的 math.sqrt() 不起作用。我需要 sqhare root 2*x-1。如代码所示
import matplotlib.pyplot as plt
import math
def f(x):
return math.sqrt(2*x-1)
plt.plot([0, 1, 2, 3, 4], [f(0), f(1), f(2), f(3), f(4)])
plt.show()
它给出了一个错误,指出 ValueError:数学域错误。我不确定如何正确地做到这一点 way/fix 它
这不起作用,因为您正试图找到负数的根,从数组中删除“0”可以让代码 运行 没有错误。
我正在尝试使用 matplotlib 和微积分绘制图形,但我的 f(x) 函数的 math.sqrt() 不起作用。我需要 sqhare root 2*x-1。如代码所示
import matplotlib.pyplot as plt
import math
def f(x):
return math.sqrt(2*x-1)
plt.plot([0, 1, 2, 3, 4], [f(0), f(1), f(2), f(3), f(4)])
plt.show()
它给出了一个错误,指出 ValueError:数学域错误。我不确定如何正确地做到这一点 way/fix 它
这不起作用,因为您正试图找到负数的根,从数组中删除“0”可以让代码 运行 没有错误。