2012-10-23 6 views
1
def evaluatePoly(poly, x): 
    ''' 
    Computes the value of a polynomial function at given value x. Returns that 
    value as a float. 

    poly: list of numbers, length > 0 
    x: number 
    returns: float 
    ''' 
    for n in range(len(poly)): 
     poly[n] = (poly[n]) * (x**n) 

    return float(sum(poly[:])) 

def computeDeriv(poly): 
    ''' 
    Computes and returns the derivative of a polynomial function as a list of 
    floats. If the derivative is 0, returns [0.0]. 

    poly: list of numbers, length > 0 
    returns: list of numbers (floats) 

    >>> print computeDeriv([-13.39, 0.0, 17.5, 3.0, 1.0]) 
    [0.0, 35.0, 9.0, 4.0] 
    >>> print computeDeriv([6, 1, 3, 0]) 
    [1.0, 6.0, 0.0] 
    >>> print computeDeriv([20]) 
    [0.0] 

    ''' 
    if len(poly) == 1: 
     poly = [0.0] 
     return poly 
    for m in range(len(poly)): 
     poly[m] = float(m) * poly[m] 
    return poly[1:] 

def computeRoot(poly, x_0, epsilon): 
    ''' 
    Uses Newton's method to find and return a root of a polynomial function. 
    Returns a list containing the root and the number of iterations required 
    to get to the root. 

    poly: list of numbers, length > 1. 
     Represents a polynomial function containing at least one real root. 
     The derivative of this polynomial function at x_0 is not 0. 
    x_0: float 
    epsilon: float > 0 
    returns: list [float, int] 

    >>> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001) 
    [0.806790753796352, 7] 
    >>> print computeRoot([1, 9, 8], -3, .01) 
    [-1.0000079170005467, 5] 
    >>> print computeRoot([1, -1, 1, -1], 2, .001) 
    [1.0002210630197605, 4] 
    ''' 
    x = x_0 
    iter = 0 
    list = [] 
    polyStart = poly[:] 
    while abs(evaluatePoly(poly, x)) >= epsilon: 
     poly = polyStart[:] 
     l = evaluatePoly(poly,x) 
     if abs(l) < epsilon: 
      list.append(x) 
      list.append(iter) 
      return list 
     else: 
      poly = polyStart[:] 
      d = computeDeriv(poly) 
      dn = evaluatePoly(d, x) 
      x = (x - (l/dn)) 
      iter = iter + 1 
+3

코드가 표시되지만 질문이 표시되지 않습니다. –

+0

http://codereview.stackexchange.com/에서 질문하십시오. 숙제를위한 특정 문제 –

+0

을 가진 질문이 아니기 때문에 http://codereview.stackexchange.com/에서 질문 할 수 있습니다. 그리고 자동 검사기가 나에게 결과 없음을주었습니다. 하지만, 파이썬은 나에게 모든 정답을주고있다. 왜 자동 검사기가 없음이됩니까? – Kevin

답변

0

나는이 기능은 일부 특정 입력에 None을 반환 의미 가정

def computeRoot(poly, x_0, epsilon): 
    x = x_0 
    iter = 0 
    list = [] 
    polyStart = poly[:] 
    while abs(evaluatePoly(poly, x)) >= epsilon: 
     poly = polyStart[:] 
     l = evaluatePoly(poly,x) 
     if abs(l) < epsilon: 
      list.append(x) 
      list.append(iter) 
      return list 
     else: 
      poly = polyStart[:] 
      d = computeDeriv(poly) 
      dn = evaluatePoly(d, x) 
      x = (x - (l/dn)) 
      iter = iter + 1 

어떻게 알 수 있습니까? 단 하나의 return이 있기 때문에 함수의 끝 부분에 있지 않습니다. abs(evaluatePoly(poly, x)) >= epsilonFalse 인 경우 while 루프가 끝나고 아무 것도 없으므로 함수가 종료되고 기본적으로 None을 반환합니다. 명시 적으로 return이 아닌 함수는 None을 반환합니다.

따라서 올바른 반환 값이 무엇인지 파악하고 return 문과 함수의 끝을 추가해야합니다.

+0

이것이 if 루프를 추가 한 이유입니다. while 루프는 무한이었고 이유를 알 수 없었습니다. 그것의 중복에 대해 안다.하지만 while 루프가 왜 엡실론보다 덜 멈추지 만 for 루프는 멈추는 지 알 수 없었다. – Kevin

+0

검사기에서 while 루프를 닫았습니다. while 루프를 닫았습니다. list.append (x) list.append (iter) return list. Brendan에게 감사드립니다. – Kevin

+0

@Kevin 첫 번째 추측이 맞을 때 의도적으로 값을 사용한다면 어떨까요? 'while' 루프는 절대 입력하지 않으므로 내부 체크가 도움이되지 않습니다. –

관련 문제