PYTHON Implement the compute_root function. This function applies Newton’s metho
ID: 3888836 • Letter: P
Question
PYTHON
Implement the compute_root function. This function applies Newton’s method of successive approximation as described above to find a root of the polynomial function. It takes in a tuple of numbers poly, an initial guess x_0, and an error bound epsilon. It returns a tuple. The first element is the root of the polynomial represented by poly; the second element is the number of iterations it took to get to that root.
The function starts at x_0. It then applies Newton’s method. It ends when it finds a root x such that the absolute value of f(x) is less than epsilon, i.e. f(x) is close enough to zero. It returns the root it found as a float.
http://www.codesend.com/view/42cef11cbe6681ccdfb695588fe40579/
Any idea??
Explanation / Answer
Code:
def f(poly, x):
i = 0.0;total = 0.0
for number in poly:
s = 0.0;m = 1.0;j = 0.0
while(j < i):
m = m * x
j = j + 1
i = i + 1
s = number * m
total = total + s
return total
def g(poly, x ,m):
i = 0.0;total = 0.0
m[1] += 1
for number in poly:
s = 0.0
m = 1.0
s = number * i
j = 0
while(j < i - 1):
m = m * x
j = j + 1
i = i + 1
s = s * m
total = total + s
return total
def compute_root(poly, x_0, epsilon):
m = [0, 0]
while(abs(f(poly,x_0))>epsilon):
x_0 = x_0 - (f(poly,x_0)/g(poly,x_0,m))
m[0] = x_0
return m
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.