Question: Using the Newton - Raphson iteration find the root of the equation x + x 2 = 7 . With an initial guess of x

Using the Newton-Raphson iteration find the root of the equation x+x2=7. With an initial guess of x=7 compute the number of iterations required for the error in the root to be below 1.0e6.
This is my code, the root is correct but the number of iterations is off by one (it should be 5 but I keep getting 6):
import math
def f(x):
return math.sqrt(x)+ x**2-7
def df(x):
return 1/(2* math.sqrt(x))+2* x
def newton_raphson(x0, tol, full_ouptut=True):
num_iter =0
error = tol +1
while error > tol:
x1= x0-(f(x0)/ df(x0))
error = abs(x1- x0)
x0= x1
num_iter +=1
return x0, num_iter
x1=7
tol =1e-6
root3, num_iter = newton_raphson(x1, tol, num_iter)
print("Newton-Raphson method root and num_iter:", root3, num_iter)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!