1

我试图用 matplotlib 和微积分绘制图表,但我的 f(x) 函数的 math.sqrt() 不起作用。我需要平方根 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: math domain error。我不确定如何以正确的方式进行/修复

4

1 回答 1

1

这不起作用,因为您试图找到负数的根,从数组中删除“0”可以让代码无错误地运行。

于 2018-10-16T20:48:41.827 回答