Friday, 24 August 2012

Numerical Technique - Newton Raphson

# include <stdio.h>
# include <math.h>

float function(float x)
{
          return((float)(x*log10(x)-1.2));
}

float derivative(float x)
{
          return((float)(log10(x)+1));
}

void main()
{
          int i;
          float a,b,c,e;
         
          printf("\nThis Program will find the Root of
 the Equation f(x) = x.logx - 1.2\n");
          printf("\n\t******* By Newton Raphson Method ********\n");
          printf("\nEnter the range in which root lies..............\n");
          printf("\nEnter the value of lower limit - ");
          //lower limit = 2
          scanf("%f",&a);
          fflush(stdin);
          printf("\nEnter the value of upper limit - ");
          //upper limit = 3
          scanf("%f",&b);
          fflush(stdin);
          printf("\nEnter the limit of error allowed - ");
          scanf("%f",&e);
          fflush(stdin);
          printf("\nThe value of function at %f is = %f",a,function(a));
          printf("\nThe value of function at %f is = %f\n",b,function(b));
          printf("\nIterations Starts now............................\n");

          c=(a+b)/2;

          for(i=1;fabs(function(c))>e;i++)
          {
                   printf("\nThe value of function at %f is = %f",c,function(c));
                  
                   c=c-(function(c)/derivative(c));
          }
          printf("\n");
          printf("\nThe Approximate root of the above function is = %f \n
and the value of function is = %f\n",c,function(c));
          printf("\nThe number of Iterations are = %d",i);
}

No comments:

Post a Comment