Root finding method
Dr. Santosh Prasad Gupta
Assistant Professor
Department of Physics
Patna University, Patna
4/10/2025 Department of Physics, PU: SP Gupta 1
Algebraic function are the one which can be represented in the form
of polynomials like 𝑓 𝑥 = 𝑎1 𝑥 3 + 𝑎2 𝑥 2 + 𝑎3 𝑥 5+ 𝑎4 𝑥 9 where 𝑎1 , 𝑎2 , 𝑎3 ,
𝑎4 , … are constants and x is a variable.
Transcendental function are non algebraic functions, for example
𝑓 𝑥 = x sin 𝑥 − 3 𝑜𝑟 𝑓 𝑥 = 𝑒 𝑥 + 𝑥 2 𝑜𝑟 𝑓 𝑥 = ln 𝑥 + 𝑥
Important facts
Theorem: An equation f(x)=0, where f(x) is a real continuous
function, has at least one root between xI and xII if
f(xl) * f(xII) < 0.
As it is clear from the above figure, there is a root exists between the
points (xI and xII). It should be noted that the function is real,
continuous.
4/10/2025 Department of Physics, PU: SP Gupta 2
However, If function does not change sign for two points, roots of the
equation may or may not be exist between the two points. The
following figures give the clear understanding about the above
statement.
✓ The function does not change sign for
the two points as shown in the figure,
but roots of the function are present
between these two points
✓ The function does not change sign for
the two points as shown in the figure,
and also the root of the function does not
exist between these two points
4/10/2025 Department of Physics, PU: SP Gupta 3
Newton–Raphson Method
In numerical analysis, Newton's method, also known as
the Newton–Raphson method, named after Isaac
Newton and Joseph Raphson, is a root-finding algorithm which
produces successively better approximations to the roots (or zeroes)
of a real-valued function.
It is based on the geometry of a curve, using the tangent lines to a
curve. As such, it requires calculus, in particular differentiation.
Roughly, the idea of Newton's
method is as follows. We seek a
solution to f(x)=0. That is, we want
to find the red dotted point in the
picture below.
4/10/2025 Department of Physics, PU: SP Gupta 4
We start with an initial guess x1.
We calculate f(x1). If f(x1)=0, we
are very lucky, and have a
solution. But most likely f(x1) is
not zero. Let f(x1)=y1, as shown.
We now try for a better guess. How to
find that better guess? The trick of
Newton's method is to draw a tangent
line to the graph y=f(x) at the
point (x1,y1)
This tangent line is a good linear
approximation to f(x) near x1, so our
next guess x2 is the point where the
tangent line intersects the x-axis, as
shown in the figure.
4/10/2025 Department of Physics, PU: SP Gupta 5
We then proceed using the
same method. We
calculate y2=f(x2); if it is
zero, we're finished. If not,
then we draw the tangent
line to y=f(x) at (x2,y2),
and our next guess x3 is
the point where this
tangent line intersects
the x-axis. As shown in
the figure
Continuing in this way, we find points x1, x2, x3, x4, … approximating
a solution. This method for finding a solution is Newton-Raphson’s
method.
As we'll see, Newton-Raphson’s method can be a very efficient method
to approximate a solution to an equation — when it works.
4/10/2025 Department of Physics, PU: SP Gupta 6
Newton–Raphson Method : Key calculation
As our introduction above just showed, the key calculation in each
step of Newton-Raphson's method is to find where the tangent line
to y=f(x) at the point (x1,y1) intersects the x-axis.
Let's find this x-intercept. The tangent line we are looking for
passes through the point (x1,y1) and has gradient f′(x1).
The line has gradient f′(x1), and passes through (x1,y1), so has
equation
(y−y1)
= f ′ x1
(x−x1)
or equivalently,
y = f ′ x1 x − x1 + y1
Setting y=0, we find the x-intercept as
y1 f(x1)
x = x1 − ′ = x1 − ′
f x1 f x1
4/10/2025 Department of Physics, PU: SP Gupta 7
Newton–Raphson Method : Algorithm
We can now describe Newton-Raphson's method algebraically.
Starting from x1, the above calculation shows that if we construct
the tangent line to the graph y=f(x) at x=x1, this tangent line
has x-intercept given by
f(x1)
x2 = x1 − ′
f x1
Then, starting from x2 we perform the same calculation, and
obtain the next approximation x3 as
f(x2)
x3 = x2 − ′
f x2
The same calculation applies at each stage: so from the n'th
approximation xn, the next approximation is given by
f(xn)
xn+1 = xn − ′
f xn
4/10/2025 Department of Physics, PU: SP Gupta 8
Newton–Raphson Method : some comments about Algorithm
✓ Often, Newton-Raphson's method works extremely well,
and the xn converge rapidly to a solution. However, it's
important to note that Newton-Raphson's method does not
always work. Several things can go wrong, as we will see
shortly.
✓ Note that if f(xn)=0, so that xn is an exact solution of f(x)=0,
then the algorithm gives xn+1=xn, and in fact all
of xn,xn+1,xn+2,xn+3,…, will be equal. If you have an
exact solution, Newton-Raphson's method will stay on that
solution!
✓ While the bisection method only requires f to be
continuous, Newton-Raphson's method requires the
function f to be differentiable. This is necessary for f to have
a tangent line.
4/10/2025 Department of Physics, PU: SP Gupta 9
Newton–Raphson Method : Algorithm
1. Start
2. Define function as f(x)
3. Define first derivative of f(x) as g(x) (df(x))
4. Input initial guess (x0), tolerable error (e) and maximum iteration (N)
5. Initialize iteration counter i = 1
6. If g(x0) = 0 then print "Mathematical Error" and goto (12) otherwise goto (7)
7. Calcualte x1 = x0 - f(x0) / g(x0)
8. Increment iteration counter i = i + 1
9. If i >= N then print "Not Convergent" and goto (12) otherwise goto (10)
10. If |f(x1)| > e then set x0 = x1 and goto (6) otherwise goto (11)
11. Print root as x1 12. Stop
4/10/2025 Department of Physics, PU: SP Gupta 10
Newton–Raphson Method : Python script
#Python code for Newton Raphson (NR)Method: f(x) = sin(x)
import math
Function is: 𝒇 𝒙 = 𝒔𝒊𝒏(𝒙)
def f(x):
return math.sin(x)
def df(x):
return math.cos(x)
x = eval(input('Enter the initial guess:'))
h = f(x)/df(x)
count=1
while (abs(h) >= 0.0001):
h = f(x)/df(x)
print('x is :','%0.8f'%x,'and f(x) is :','{:.6e}'.format(f(x)), 'and Iteration is :',count)
if(abs(f(x))<=0.0000001):
break
x=x-h
count=count+1
print('The value of the root is :','%.8f' % x)
4/10/2025 Department of Physics, PU: SP Gupta 11
Newton–Raphson Method : output
Enter the initial guess:1.2
x is : 1.20000000 and f(x) is : 9.320391e-01 and Iteration is : 1
x is : -1.37215162 and f(x) is : -9.803349e-01 and Iteration is : 2
x is : 3.59557218 and f(x) is : -4.385454e-01 and Iteration is : 3
x is : 3.10759952 and f(x) is : 3.398659e-02 and Iteration is : 4
x is : 3.14160575 and f(x) is : -1.309946e-05 and Iteration is : 5
The value of the root is : 3.14159265
Enter the initial guess:0.2
x is : 0.20000000 and f(x) is : 1.986693e-01 and Iteration is : 1
x is : -0.00271004 and f(x) is : -2.710032e-03 and Iteration is : 2
x is : 0.00000001 and f(x) is : 6.634451e-09 and Iteration is : 3
The value of the root is : 0.00000001
Enter the initial guess:4.2
x is : 4.20000000 and f(x) is : -8.715758e-01 and Iteration is : 1
x is : 2.42222023 and f(x) is : 6.589127e-01 and Iteration is : 2
x is : 3.29817841 and f(x) is : -1.559466e-01 and Iteration is : 3
x is : 3.14030020 and f(x) is : 1.292458e-03 and Iteration is : 4
x is : 3.14159265 and f(x) is : -7.196615e-10 and Iteration is : 5
The value of the root is : 3.14159265
4/10/2025 Department of Physics, PU: SP Gupta 12
// Newton Raphson (NR)Method: f(x) = sin(x)
Newton–Raphson Method : C code
#include<stdio.h>
#include<math.h>
#define f(x) sin(x)
#define g(x) cos(x)
main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
printf("Mathematical Error.");
}
x1 = x0 - f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
x0 = x1;
step = step+1;
if(step > N)
{
printf("Not Convergent.");
}
f1 = f(x1);
}while(fabs(f1)>e);
printf("\nRoot is: %f", x1);
}
4/10/2025 Department of Physics, PU: SP Gupta 13
Assignments
Question 1: Find the roots of the function: 𝑓 𝑥 = 𝑥 2 − 8 ; by using
Bisection and Newton-Raphson method.
Question 2: Find the roots of the function: 𝑓 𝑥 = 𝑥 3 − 19 ; by using
Bisection and Newton-Raphson method.
Question 3: Find the roots of the function: 𝑓 𝑥 = 𝑆𝑖𝑛 𝑥 2 ; by using
Bisection and Newton-Raphson method.
Question 4: Find the roots of the function: 𝑓 𝑥 = 𝑆𝑖𝑛 𝑥 2 + log 𝑥 − 𝑥 5 ; by
using Bisection and Newton-Raphson method.
Question 5: Find the root of the function: 𝑓 𝑥 = 𝑥 2 − 10 ; between x = 0
and x = 5, by using Bisection and Newton-Raphson method.
4/10/2025 Department of Physics, PU: SP Gupta 14