C code for newton-raphson method

Problem: Here we have to find root for the polynomial x^3-8*x-4 upto 6D(decimal places)

Solution in C:

#include<stdio.h>
#include<math.h>
#define f(x) pow(a,3)-8*a-4;
#define fd(x) 3*pow(a,2)-8;
int main()
{
    double a,b,c,d,h,k,x,y;
    int i,j,m,n;
    printf("Enter the value of xn: ");
    scanf("%lf",&a);
    printf("Enter itteration number: ");
    scanf("%d",&n);
    printf("  xn           f(x)         f'(x)       hn=-f(x)/f'(xn)   xn+1=xn+h\n");
    printf("-----------------------------------------------------------------------------------------\n");
    for(i=1;i<=n;i++)
    {
    x=f(a);
    y=fd(x);
    h=-(x/y);
    k=h+a;
    printf(" %.7lf     %.7lf     %.7lf     %.7lf     %.7lf\n",a,x,y,h,k);
a=k;
    }
     printf("\nThe approximation to the root is %.6lf which is upto 6D",k);
    return 0;

}

 Output:
2014-11-27 11_48_33-

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *