Lab No.
07
Student Name/Roll No:
Lab Instructor Signatures: Date:
Objective: To locate the roots of real valued non-linear continuous function using Newton
Raphson method.
Tools and Equipment:
Laptop/desktop/workstation with anaconda installed (python 3.9 or later)
Newton-Raphson method (named after Isaac Newton and Joseph Raphson)
Newton-Raphson method, also known as Newton’s method, is a popular iterative method to
locate the root of a polynomial equation. It is also considered as limiting case of Secant method.
The theoretical and mathematical background behind Newton-Raphson method is approximation
of the given function by tangent line with the help of derivative, after choosing a guess value of
root which is reasonably close to the actual root. The x - intercept of the tangent is calculated by
using elementary algebra, and this calculated x - intercept is typically better approximation to the
root of the function. This procedure is repeated till the root of desired accuracy is found.
Figure: 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 )
Now generalize the above equation
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
while iteration < max_iterations:
f_x = func(x)
f_prime_x = func_derivative(x)
# Check for division by zero
if f_prime_x == 0:
print(f"Derivative is zero at iteration {iteration}.
The method cannot proceed.")
return None, iteration, False
# Update x using the Newton-Raphson formula
x_new = x - f_x / f_prime_x
# Check for convergence
if abs(x_new - x) < epsilon:
return x_new, iteration + 1, True
x = x_new
iteration += 1
print(f"The method did not converge within {max_iterations}
iterations.")
return None, iteration, False
# Example usage: Populate the table with varying initial guesses
and error levels
results = []
initial_guesses = [-11, -10, -5, 0, 5] # Different initial
guesses
error_levels = [0.1, 0.01, 0.001] # Different allowed errors
for guess in initial_guesses:
for error in error_levels:
root, iterations, success = newton_raphson(guess,
epsilon=error)
if success:
[Link]((iterations, guess, round(root, 5),
error))
# Print the table
print(f"{'No. of iterations':<20}{'Initial guess':<15}
{'Approximated Root':<20}{'Error allowed':<15}")
print("-" * 70)
for result in results:
print(f"{result[0]:<20}{result[1]:<15}{result[2]:<20}
{result[3]:<15}")
No. of iterations Initial guess Approximated Root Error allowed
5 -11 -0.09548 0.1
6 -11 -0.09549 0.01
7 -11 -0.09549 0.001
4 -10 -0.09549 0.1
5 -10 -0.09549 0.01
6 -10 -0.09549 0.001
3 -5 -0.09549 0.1
4 -5 -0.09549 0.01
5 -5 -0.09549 0.001
2 0 -0.09549 0.1
3 0 -0.09549 0.01
4 0 -0.09549 0.001
2 5 -0.09549 0.1
Review Questions
Q1: What are the usages of the Newton-Raphson Method?
Solves nonlinear equations in engineering, physics, and optimization.
Used in structural analysis, power flow problems, and finding roots of polynomials.
Applied in machine learning for loss minimization.
Q2: Does Newton-Raphson always converge?
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.
Q3: When will the Newton-Raphson method fail?
f′(x)=0f'(x) = 0 (division by zero).
Discontinuous or non-differentiable functions.
Poor initial guesses or complex roots.
Multiple roots cause slow or no convergence.