C program for Newton-Raphson method for solving a non-linear equation

Share

Assume given function :

f(x) = x2+4x-9 and f'(x) = 2x+4

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

float f(float X){
    return X*X + 4*X - 9;
}

float fdash(float X){
    return 2*X + 4;
}

int main()
{
    float x0,x1,fx0,fdashx0,E,ER=0;
    
    printf("X0 : ");
    scanf("%f",&x0);
    
    
    printf("E : ");
    scanf("%f",&E);
    
    do{
        fx0 = f(x0);
        fdashx0 = fdash(x0);
        
        x1 = x0 - (fx0/fdashx0);
        ER = fabs((x1-x0)/x1);
        x0 = x1;
        
    }while(E<ER);
    
    printf("Root : %.3f",x1);

    return 0;
}

C Program for Secant Method to solve a non-linear equation.

C Program for Bisection Method to solve a non-linear equation

Share
Sudeep Mishra

Sudeep Mishra

Healing

Leave a Reply

Your email address will not be published.

%d bloggers like this: