Newton raphson method in c


  👉 NEWTON RAPHSON METHOD:



Let, x0 be an approximation of a root of the given equation f(x)=0, which may be algebraic or transcendental.

Let x0+h be the exact value or the better approximation of the corresponding root,h being a small quantity.

Then,f(x0+h)=0.
Expanding it by Taylor`s theorem,we get,
f(x0+h)=f(x0)+hf `(x0)+h*h f ``(x0)+..........=0

h=-f(x0) / f `(x0)

x1=x0+h
x2=x1+h

condition for convergence of newton raphson method:
 |1-(f `(x)*f `(x))-[ f(x)*f ``(x)] /f `(x)*f `(x)|   <1

|f(x)*f ``(x)|<|f `(x)*f `(x)|

coding:

  1. #include<stdio.h>
  2. #include<math.h>
  3. float fun(float x)
  4. {
  5.     return x*x-2*x+3;
  6. }
  7. float diff1fun(float x)
  8. {
  9.     return 2*x-2;
  10. }
  11. void main()
  12. {
  13.     int itr, maxmitr;
  14.     float h, x0, x1, error;
  15.     printf("\nEnter x0"); 
  16.     scanf("%f", &x0);
  17.     printf("\nEnter maximum ittreation\n"); 
  18.     scanf("%d",&maxmitr);
  19.     printf("\nEnter error"); 
  20.     scanf("%f",&error);
  21. for (itr=1; itr<=maxmitr; itr++)
  22.     {
  23.         h=fun(x0)/diff1fun(x0);
  24.         x1=x0-h;
  25.         printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
  26.         if (fabs(h) < error)
  27.         {
  28.             printf("After %3d iterations, root = %8.6f\n", itr, x1);
  29.             return 0;
  30.         }
  31.         x0=x1;
  32.     }
  33.     printf("iterations are insufficient\n");
  34.     return 1;
  35. }


output:



2 comments:

Copyright © 2013 free coding