Skip to content
DeepForgeby svx
All problems
MediumCalculusca-023

Newton Method n Steps

Problem

Run n iterations of the Newton-Raphson update x = x - f(x) / f'(x) for f(x) = sum(coeffs[i] * x^i), starting from x0.

Return the final iterate. With n = 0 the starting point is returned unchanged.

Hint

Newton's method converges quadratically, so a handful of steps gets very close to the root.

Test cases

case 1
in: [[-2,0,1],1,3]
expected: 1.4142156862745099
case 2
in: [[-2,-1,0,1],1,2]
expected: 1.6363636363636365
case 3
in: [[0,0,1],3,0]
expected: 3
case 4
in: [[-9,0,1],1,5]
expected: 3
Starter codePython
def newton_iterate(coeffs, x0, n):
    # Your code here
    pass

4 test cases · run your code against them in the editor