gpt4 book ai didi

python - math.sqrt() 值错误 : math domain error

转载 作者:行者123 更新时间:2023-12-01 08:54:53 28 4
gpt4 key购买 nike

我试图用 matplotlib 和微积分绘制一个图形,但是我的 f(x) 函数的 math.sqrt() 不起作用。我需要对 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: 数学域错误。我不知道如何以正确的方式做到这一点/修复它

最佳答案

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

关于python - math.sqrt() 值错误 : math domain error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843617/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com