简体   繁体   English

math.sqrt() ValueError: 数学域错误

[英]math.sqrt() ValueError: math domain error

Im trying to plot out a graph with matplotlib and calculus, but the math.sqrt() of my f(x) function doesnt work.我试图用 matplotlib 和微积分绘制一个图形,但我的 f(x) 函数的 math.sqrt() 不起作用。 I need to sqhare root 2*x-1.我需要平方根 2*x-1。 As seen in the code如代码所示

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()

And it gives an error saying ValueError: math domain error.它给出了一个错误,说 ValueError: math domain error。 Which i am not sure how to do it the right way/fix it我不知道如何以正确的方式/修复它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM