Newton Raphson Method

/*
Write a C program to solve algebraic equation x3-8x-4=0 by using Newton Raphson Method.
*/
#include<stdio.h>
#include<math.h>
float f(float);
float df(float);
main()
{
    float a,x,h,err=0.00001;
    printf("\nEnter initial value=");
    scanf("%f",&a);
    x=a;
    do
    {
        h=-(f(x)/df(x));
        a=x;
        x=x+h;
    }while(fabs(x-a)>=err);
    printf("\nThe approximate value is %f",x);
}

float f(float x)
{
    return(x*x*x-8*x-4);
}

float df(float x)
{
    return(3*x*x-8);
}

No comments:

Post a Comment