简体   繁体   English

Python找到曲线长度

[英]Python finding a curve Length

I need to find the curve length using 3 different target functions (below) and the function i defind as findLength. 我需要使用3个不同的目标函数(如下)和我定义为findLength的函数来找到曲线长度。 I ran through it I could not get any numbers can anyone tell me what is wrong it this code and I am very new to python and this is for school assignment 我跑遍了它,但我没有得到任何数字。有人能告诉我这段代码有什么问题吗?我是python的新手,这是学校作业

def showLength(targetFunc, minPoints, maxPoints):
    while minPoints <= maxPoints:
       x_ = (targetFunc(minPoints)[0][0])
       y_ = (targetFunc(minPoints)[1])
       length = findLength(x_, y_)

       print('Length of the curve of the function %s ' % targetFunc.__name__)
       print("#POINTS       LENGTHS")
       print(" %f              %.4d" % (minPoints, length )  )

       minPoints = minPoints * 2 


def findLength(xs,ys):
    xVal = np.array(xs)
    yVal = np.ndarray(ys)
    length = np.sqrt((xVal - (xVal-1))**2 + (yVal - (yVal - 1))**2 )
    return length


def xSinx(numPoints):
    MIN = -20
    MAX = 20
    xValues = np.linspace(MIN, MAX, numPoints)
    yValues = xValues * np.sin(xValues)

    return xValues, yValues


def halfCircle(numPoints):
    MIN = -5
    MAX = 5
    xValues = np.linspace(MIN, MAX, numPoints)
    yValues = np.sqrt(25 - xValues ** 2)

    return xValues, yValues


def fractionalPowers(numPoints):
    assert type(numPoints) is int and numPoints > 1, "invalid numPoints"
    MIN = 0
    MAX = 100
    xValues = np.linspace(MIN, MAX, numPoints)
    yValues = np.array(xValues**0.25 + (xValues**(1/3)) + np.sqrt(xValues))

    return xValues, yValues

I'm not sure how you are running your code, but if you simply ran your script as you listed, you wouldn't get any result. 我不确定您如何运行代码,但是如果您只是按照列出的顺序运行脚本,则不会得到任何结果。 Your script defines a bunch of functions but does not call them. 您的脚本定义了许多函数,但未调用它们。

To run findLength() , you could add the following to the bottom of your script: 要运行findLength() ,可以将以下内容添加到脚本的底部:

xdata = [0, 1, 2]
ydata = [3, 2, 6]

result = findLength(xdata, ydata)

print "The result is: %d" % result

or better yet: 或更好:

if __name__ == "__main__":
    xdata = [0, 1, 2]
    ydata = [3, 2, 6]

    result = findLength(xdata, ydata)

    print "The result is: %d" % result

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

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