Computer Science‎ > ‎

C Program: Numerical Computing - Implementing the Newton Raphson Method

C Program implementing the Newton Raphson Method (Numerical Computing) for a function


/*This program in C illustrates the Newton Raphson method.
This program calulate the approximation to the root of x*x-5.
The maximum error between 2 succesive approximation is taken as input from the user
alongwith the maximum number of iterations and the initial approximation.
*/
#include<stdio.h>
#include<math.h>
double F(double x)
{//our function
    return ((x)*(x)-5);
}
double Fd(double x)
{//function's differentiation
    return (2*(x));
}
int main()
{
    double x0,h,err,root,x1;
    int miter,iter;
    printf("Enter the first approximation ,the max error and the maximum number of iterations\n");
    scanf("%lf%lf%d",&x0,&err,&miter);
    iter=1;
    while(iter<=miter)
    {
        h=F(x0)/Fd(x0);//calculatinf f(x)/f'(x)as we do in Newton Raphson method
        x1=x0-h;//x1=x0-f(x)/f'(x)
        printf("The approximation's value after %d iteration is %.12lf\n",iter,x1);
        if(fabs(h)<err)//If the difference between the 2 approximations is below the max error
        {
            root=x1;//then make the approximation as the root
            break;
        }
        else
        x0=x1;
        iter++;
    }
    if(root==x1)
    {//display root and the function value
        printf("The root is: %.12lf\n",root);
        double fncvalue = F(root);
        printf("Value of F(root) is: %.12lf",fncvalue);
    }
    else
    printf("The unsufficent number of iteration");//In case root!=x1 then number of iteration were insufficient
    getch();
}
/*A sample run of thje program was carried out and the results were found to be as:-
Enter the first approximation ,the max error and the maximum number of iterations
2
0.0000000001
7
The approximation's value after 1 iteration is 2.250000000000
The approximation's value after 2 iteration is 2.236111111111
The approximation's value after 3 iteration is 2.236067977916
The approximation's value after 4 iteration is 2.236067977500
The approximation's value after 5 iteration is 2.236067977500
The root is: 2.236067977500
Value of F(root) is: 0.000000000000
*/


C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers