Python找到一条曲线长度

时间:2016-12-15 23:28:11

标签: python variable-assignment curve

我需要使用3个不同的目标函数(下面)找到曲线长度,并且我需要将函数定义为findLength。我跑过去了,我无法得到任何数字,任何人都可以告诉我这个代码有什么不对,我对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

1 个答案:

答案 0 :(得分:-1)

我不确定您是如何运行代码的,但如果您只是按照列出的那样运行脚本,则不会得到任何结果。您的脚本定义了一堆函数但不调用它们。

要运行findLength(),您可以将以下内容添加到脚本的底部:

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

result = findLength(xdata, ydata)

print "The result is: %d" % result

或更好:

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

    result = findLength(xdata, ydata)

    print "The result is: %d" % result