The Newton-Raphson method (also known as Newton’s method) is a way to quickly find a good approximation for the root of a real-valued function f(x)=0. It uses the idea that a continuous and differentiable function can be approximated by a straight line tangent to it.
Newton-Raphson Method Solved Example
C Program
//Newton-Raphson Method
//This Code is written by Souvik Ghosh
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
/* Defining equation to be solved.
Change this equation to solve another problem. */
#define f(x) x*x-4*x-7
/* Defining derivative of g(x).
As you change f(x), change this function also. */
#define g(x) 2*x-4
//main function
void main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
/* Inputs */
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
/* Implementing Newton Raphson Method */
printf("\nStep\t\tx0\t\tf0\t\tg0\t\tx1\n");
do
{
f0 = f(x0);
g0 = g(x0);
if(g0 == 0.0)
{
printf("Mathematical Error.");
break;
}
x1 = x0 - f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,g0,x1);
x0 = x1;
step++;
if(step > N)
{
printf("Not Convergent.");
break;
}
f1 = f(x1);
}while(fabs(f1)>e);
printf("\nThe Root is: %0.4f", x1);
}