Newton Raphson method in C & C++

The Newton-Raphson method, also called Newton’s method is used to find an approximate root of a real-valued polynomial, f(x) = 0. It works on the idea that a straight-line tangent can be drawn to a continuous and differentiable function which can be used to approximate the root of the function. Newton-Raphson works in several steps in which it successively finds better approximations of the root of the function. An initial guess value of the root has to be provided and then using this method, that value can be used to better approximate the root. This initial guess is a value which is close to the exact root value. The formula for approximation of root is:

, where x0 is the initial guess of the root and x1 is the new approximated value of the root.

Newton Raphson Method Program in C

/* *********************
Author: Exploring Bits
********************* */
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define TRUE 1
double inputPolynomial (double coeff[], double n, double x){
double fx = 0.0, temp;
int i;
for (i = n; i >= 0; i–){
temp = coeff[i];
if (i == 0)
fx = fx + temp;
else
fx = fx + (pow(x, i) * temp);
}
return fx;
}
double getDerivative (double coeff[], double n, double x, double vfx){
double dfx = 0, temp;
int i;
for (i = n; i >= 0; i–){
temp = coeff[i];
if (i == 1){
dfx = dfx + temp;
return dfx;
}
else
dfx = dfx + (pow(x, i-1) * temp * i);
}
}
void newtonRaphson (){
int n, i;
double coeff[100], temp, x, fx, dfx, h;
printf(“Enter the highest degree of the polynomial: “);
scanf(“%d”, &n);
for (i = n; i >= 0; i–){
printf(“Enter the coefficient of %d degree: “, i);
scanf(“%lf”, &coeff[i]);
}
printf(“Enter the initial assumed value of x: “);
scanf(“%lf”, &x);
while(TRUE){
fx = inputPolynomial(coeff, n, x);
dfx = getDerivative(coeff, n, x, fx);
h = fx / dfx;
if (fabs(h) < 0.00001)
break;
x = x – h;
}
printf(“\nThe value of the root is %lf\n”, x);
}
int main (){
newtonRaphson();
return 0;
}

 

Newton Raphson Method Program in C ++

/* *********************
Author: Exploring Bits
********************* */
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
double inputPolynomial (double coeff[], double n, double x){
double fx = 0.0;
for (int i = n; i >= 0; i–){
double temp = coeff[i];
if (i == 0)
fx = fx + temp;
else
fx = fx + (pow(x, i) * temp);
}
return fx;
}
double getDerivative (double coeff[], double n, double x, double vfx){
double dfx = 0;
for (int i = n; i >= 0; i–){
double temp = coeff[i];
if (i == 1){
dfx = dfx + temp;
return dfx;
}
else
dfx = dfx + (pow(x, i-1) * temp * i);
}
}
void newtonRaphson (){
int n;
cout << “Enter the highest degree of the polynomial: “;
cin >> n;
double coeff[n+1];
for (int i = n; i >= 0; i–){
double temp;
cout << “Enter the coefficient of “ << i << ” degree: “;
cin >> coeff[i];
}
double x;
cout << “Enter the initial assumed value of x: “;
cin >> x;
while (true){
double fx = inputPolynomial(coeff, n, x);
double dfx = getDerivative(coeff, n, x, fx);
double h = fx / dfx;
if (abs(h) < 0.00001)
break;
x = x – h;
}
cout << “\nThe value of the root is “ << x << endl;
}
int main (){
newtonRaphson();
return 0;
}

Output of Newton Raphson Method Program in C and C++

In the above program, the newtonRaphson () is responsible for taking the coefficients of the polynomials from the user. These coefficients are then fed to the inputPolynomial () and the getDerivative () functions to get the values of the function and its derivative respectively. These values are then put in the above formula to obtain a new approximation of the root. This new value then becomes the initial value for the next iteration and then an even better approximation is calculated. The more this process is repeated, the more accurate the approximation is. Here the approximation calculated is accurate up to 5 decimal places.

Leave a Comment