C program for Bisection method for solving a non-linear equation

Share

Assume given function :

f(x) = 3x2-6x+2

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

float f(float X){
    return 3*X*X - 6*X + 2;
}


int main()
{
    float lower,upper,mid,fxupper,fxlower,fxmid,E,ER=0;
    
    printf("X0/lower : ");
    scanf("%f",&lower);
    
    printf("X1/upper : ");
    scanf("%f",&upper);
    
    
    printf("E : ");
    scanf("%f",&E);
    
    do{
        ER = fabs((lower-upper)/upper);
        
        mid = (lower+upper)/2;
        fxupper = f(upper);
        fxlower = f(lower);
        fxmid = f(mid);
        
        
        if(fxlower*fxmid > 0){
            lower=mid;
        }
        else if(fxlower*fxmid==0){
            break;
        }else{
            upper=mid;
        }
        
        
    }while(E<ER);
    
    printf("Root : %.3f",mid);

    return 0;
}

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

C Program for Newton-Raphson’s 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: