Python finding a curve Length

2.4k views Asked by At

I need to find the curve length using 3 different target functions (below) and the function i defind as 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

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

There are 1 answers

2
steve chang On

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:

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