Introduction
Consider a graph below of
. Let us consider
is an approximate root of
. Draw a tangent at the curve
at
as shown in figure aside. The point of intersection of this tangent with the x-axis gives the second approximation to the root. Let the point of intersection be
. The slope of the tangent is given by;
![]()
where
is the slope of
at
. Solving for
we obtain
![Rendered by QuickLaTeX.com \[ x_2 = x_1 - {f(x_1) \over f'(x_1)} \]](https://chandanbhagat.com.np/wp-content/ql-cache/quicklatex.com-fdb920acb268143d8537384d8c29d856_l3.png)
This is called the Newton-Raphson formula.
The next approximation would be
![Rendered by QuickLaTeX.com \[ x_3 = x_2 - {f(x_2) \over f'(x_2)} \]](https://chandanbhagat.com.np/wp-content/ql-cache/quicklatex.com-8beb49851ae68729cfdbc1dd0459f301_l3.png)
So, we can generalize the next approximation using
![Rendered by QuickLaTeX.com \[ x_{n+1} = x_n - {f(x_n) \over f'(x_n)} \]](https://chandanbhagat.com.np/wp-content/ql-cache/quicklatex.com-598c00ba1aebeffcbc1c9f947833c00c_l3.png)
And finally the process is repeated until the difference between the consecutive values are within prescribed limit.

Algorithm
1. Guess initial root =
and define stopping criteria error
2. Evaluate
and ![]()
3. Compute the new root
![Rendered by QuickLaTeX.com \[ x_2 = x_1 - {f(x_1) \over f'(x_1)} \]](https://chandanbhagat.com.np/wp-content/ql-cache/quicklatex.com-fdb920acb268143d8537384d8c29d856_l3.png)
4. Set ![]()
5. Check if ![]()
goto step 3, else
6. Print root = ![]()
7. End
Code
C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x) ((x * x * x) - 18)
#define fd(x) (3 * x * x)
#define fdd(x) 6 * x
int main()
{
float x0, x1, error, errorold, converge, order;
int i = 0;
printf("Input the approximation : ");
scanf("%f", &x0);
converge = (f(x0) * fdd(x0)) / (fd(x0) * fd(x0));
if (converge > 1)
exit(1);
printf("Ite\tX0\t\tX1\t\tError\t\tOrder\n");
do
{
errorold = error;
x1 = x0 - (f(x0) / fd(x0));
if (f(x1) == 0)
{
break;
}
error = fabs(x1 - x0);
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t", ++i, x0, x1, error);
if (i == 1 || error == 0 || errorold == 1)
{
printf("-----\n");
}
else
{
order = log(error) / log(errorold);
printf("%4.6f\n", order);
}
x0 = x1;
} while (error > 0.00005);
printf("Root is %4.6f", x0);
return 0;
}