Sunday 17 March 2013

C Program for Newton-Raphson Method

Basic Theory:
The Newton-Raphson method is a powerful technique for solving equations numerically. It is based on the simple idea of linear approximation. The Newton Method, when properly used, usually comes out with a root with great efficiency. It converges, if it does, faster than other techniques. The single initial value used in this approximation is called x0. The Newton Method is usually very good if x0 is close to r, and can be horrid if it is not.

It uses the following formula for estimating next values and iterates the process until required precision is achieved.
xn+1 = xn - f(xn) / f '(xn)
It can be further explained by the following example:
We have following values,
f(x) = x^2 - 4
f '(x) = 2x
x0 = 6

We will assume that the process has worked accurately enough when our delta-x becomes less than 0.1. This value of precision is specific to each situation. A much more, or a much less, precise value may be assumed when using the Newton-Raphson method in other contexts. The table below shows the execution of the process.

n
xn
f(xn)
f '(xn)
xn+1
dx
0
x0 = 6
f(x0) = 32
f '(x0) = 12
x1 = 3.33

1
x1 = 3.33
f(x1) = 7.09
f '(x1) = 6.66
x2 = 2.27
dx = 1.06
2
x2 = 2.27
f(x2) = 1.15
f '(x2) = 4.54
x3 = 2.01
dx = .26
3
x3 = 2.01
f(x3) = 0.04
f '(x3) = 4.02
x4 = 2.00
dx = 0.01


Thus, using an initial x-value of six (6) we find one root of the equation f(x) = x2-4 is x=2. If we were to pick a different inital x-value, we may find the same root, or we may find the other one, x=-2.

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x-4*x-10
#define fd(x) 2*x-4
#define E 0.0005
void main(){

clrscr();

float x0, x1, f0, fd0, e;

printf("\t\t\t-----NEWTON-RAPHSON METHOD-----");
printf("\n\n FINDING ROOT OF X^2-4X-10");

printf("\n Enter the initial value: ");
scanf("%f", &x0);

printf("\n f(x0)\t\tfd(x0)\t\tx1\t\tError");
printf("\n -----------------------------------------------------");

begin:
f0 = f(x0);
fd0 = fd(x0);
x1 = x0 - (f0/fd0);
e =fabs((x1-x0)/x1);

                if( e < E){
                                printf("\n\n Approximate Root = %.5f", x1);
                }
                else{
                                printf("\n %.2f\t\t%.3f\t\t%.3f\t\t%.4f",f(x0),fd(x0),x1,e);
                                x0 = x1;
                                goto begin;
                }

getch();

}

Sample Output:

    -----NEWTON-RAPHSON METHOD-----
FINDING ROOT OF X^2-4X-10
Enter the initial value: 0

f(x0)         fd(x0)      x1     Error
---------------------------------
-10.00  -4.000   -2.500  1.0000
6.25     -9.000   -1.806   0.3846
0.48     -7.611   -1.742   0.0364
0.00     -7.484   -1.742   0.0003


Approximate Root = -1.74166 


6 comments:

  1. The code is wonderful.
    For the first time ever i came across a code online which executes without errors .. helped to completed my home assignment. Thank You

    ReplyDelete
  2. Great that the code worked for you. However, I suggest everyone to spend some time to understand the theory, example, and the program too. You can base your solution around my code, but do not just copy-paste the code to solve your assignments :)

    ReplyDelete
  3. Can someone help me out with a general code of Newton Raphson method that can solve a system of non linear equations with the Jacobian matrix inclusive in a general form. I have 8 model equations with 8 unknowns.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Was this post helpful? Ask any questions you have, I will try to answer them for you.