Lab07 Newton Raphson Method
Lab07 Newton Raphson Method
07
Objective: To locate the roots of real valued non-linear continuous function using Newton
Raphson method.
As shown in figure above, consider x1 to be the initial guess root of the function f(x) which is
essentially a differential function. Now, to derive better approximation, a tangent line is drawn as
shown in the figure. The equation of this tangent line is given by:
( y− y 1)
m=
(x−x 1)
' f ( x1 )
f ( x )=x−
(x 2−x 1 )
f ( x 1)
( x 2−x 1 )= y − f ' (x )
1
f (x )
1
( x 2−x 1 )= y − f ' (x ) ;(y = 0, it is on x-axis)
1
f (x 1 )
x 2=x 1−
f '(x 1 )
f (x 1)
x n+1=x n −
f ' (x 1)
Where f’(x) is the derivative of function f(x). This formula is called Newton Raphson
method/formula.
Newton Raphson Algorithm:
1. Check if the given function is differentiable or not. If the function is not differentiable,
Newton’s method cannot be applied.
2. Find the first derivative f’(x) of the given function f(x).
3. Take an initial guess root of the function, say x1.
4. Use Newton’s iteration formula to get new better approximate of the root say x2 using the
following formula, x2 = x1 – f(x1) / f’(x1)
5. Repeat the process for x3, x4… till the actual root of the function is obtained, or till
fulfilling the error tolerance criteria.
Newton Raphson Method implementation in Python
def func(x):
return x**3 - x**2 + 2
def func_derivative(x):
return 3*x**2 - 2*x
def newton_raphson(x0, epsilon=0.001, max_iterations=100):
iteration = 0
x = x0
while iteration < max_iterations:
f_x = func(x)
f_prime_x = func_derivative(x)
if f_prime_x == 0:
print("Derivative is zero. The method cannot
proceed.")
return None
x_new = x - f_x / f_prime_x
if abs(x_new - x) < epsilon:
print(f"The root of the function is approximately:
{x_new:.3f}")
print(f"Number of iterations: {iteration + 1}")
return x_new
x = x_new
iteration += 1
print("The method did not converge within the maximum number
of iterations.")
return None
# Example usage:
x0 = -20
newton_raphson(x0)
Lab Tasks
1. Improve the given code for Newton Raphson method.
2. Find roots of x 2+ 10 x +1for Newton-Raphson method.
3. Populate the following table with varying options of initial guess and level of error.
Note the number of iterations this method takes to reach the root.
def func(x):
return x**2 + 10*x + 1
def func_derivative(x):
return 2*x + 10
def newton_raphson(x0, epsilon=0.001, max_iterations=100):
iteration = 0
x = x0
x = x_new
iteration += 1
Review Questions
No, convergence depends on the initial guess, behavior of the function, and derivative conditions.
It may fail for multiple roots, poor initial guesses, or steep gradients.