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
답변
나는이 기능은 일부 특정 입력에 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)) >= epsilon
이 False
인 경우 while
루프가 끝나고 아무 것도 없으므로 함수가 종료되고 기본적으로 None
을 반환합니다. 명시 적으로 return
이 아닌 함수는 None
을 반환합니다.
따라서 올바른 반환 값이 무엇인지 파악하고 return
문과 함수의 끝을 추가해야합니다.
이것이 if 루프를 추가 한 이유입니다. while 루프는 무한이었고 이유를 알 수 없었습니다. 그것의 중복에 대해 안다.하지만 while 루프가 왜 엡실론보다 덜 멈추지 만 for 루프는 멈추는 지 알 수 없었다. – Kevin
검사기에서 while 루프를 닫았습니다. while 루프를 닫았습니다. list.append (x) list.append (iter) return list. Brendan에게 감사드립니다. – Kevin
@Kevin 첫 번째 추측이 맞을 때 의도적으로 값을 사용한다면 어떨까요? 'while' 루프는 절대 입력하지 않으므로 내부 체크가 도움이되지 않습니다. –
- 1. iterative Newton 's method
- 2. 왜 함수가 NULL을 반환합니까?
- 3. 왜 내 bash 함수가 잘못된 값을 반환합니까?
- 4. 왜 내 함수가 항상 false를 반환합니까?
- 5. 왜 함수가 잘못된 값을 반환합니까?
- 6. 왜 "return list.sort()"는 목록이 아닌 None을 반환합니까?
- 7. 왜 내 양식이 원시 PHP 코드를 반환합니까?
- 8. PHP 함수가 0을 반환합니까?
- 9. 왜 내 메서드는 항상 null을 반환합니까?
- 10. 왜 retainCount가 -1을 반환합니까?
- 11. 왜 내 수업은 첫 번째 값만 반환합니까?
- 12. 왜 내 루비 메서드가 항상 true를 반환합니까?
- 13. 왜 내 SQL 쿼리가 중복 결과를 반환합니까?
- 14. 왜 내 REST 요청이 가비지 데이터를 반환합니까?
- 15. 왜 함수가 정수 결과가 아닌 "함수"를 반환합니까?
- 16. 왜 ShowDialog는 항상 DialogResult.Cancel을 반환합니까?
- 17. 왜 파이썬은 슬라이스 단계에 None을 삽입합니까?
- 18. 이것은 왜 빈 튜플을 반환합니까?
- 19. 왜 fgets가 나쁜 문자열을 반환합니까?
- 20. 왜 null을 반환합니까?
- 21. 왜 stringForKey가 null을 반환합니까?
- 22. 왜 이것이 NaN을 반환합니까?
- 23. 왜 iHTMLelementcollection.item이 객체를 반환합니까?
- 24. 왜 cout은 smanip을 반환합니까?
- 25. 왜 문자열이 char을 반환합니까?
- 26. 왜 GetWindowThreadProcessId가 0을 반환합니까?
- 27. 왜 math.ceil은 float을 반환합니까?
- 28. 왜 내 재귀 함수가 잘못 되었습니까?
- 29. 내 함수가 비어 있음을 반환합니다. 왜?
- 30. 왜 내 자바 스크립트 함수가 작동하지 않습니다
코드가 표시되지만 질문이 표시되지 않습니다. –
http://codereview.stackexchange.com/에서 질문하십시오. 숙제를위한 특정 문제 –
을 가진 질문이 아니기 때문에 http://codereview.stackexchange.com/에서 질문 할 수 있습니다. 그리고 자동 검사기가 나에게 결과 없음을주었습니다. 하지만, 파이썬은 나에게 모든 정답을주고있다. 왜 자동 검사기가 없음이됩니까? – Kevin