Thursday, January 11, 2018

Bisection Method Program

#include
#include
#include
#define E 0.0001
#define f(x) 3*x*x+6*x-2
int mainy(){
    int count=0;
    float x0, x1, x2, f0, f1, f2, err;
    printf("Enter the value of Lower Limit: ");
    scanf("%f", &x1);
    printf("Enter the value of Upper Limit: ");
    scanf("%f", &x2);
    printf("\nLower Limit\tUpper Limit\tMid Value\n");
    f1=f(x1);
    f2=f(x2);
    if((f1*f2)>0){
        printf("Solution doesnot exist");
    }
    else{
        do{
            count++;
            x0=(x1+x2)/2;
            f0=f(x0);
            printf("%0.4f\t\t%0.4f\t\t%0.4f\n", x1,x2, x0);
            if((f1*f0)<0){
                x2=x0;
                f2=f0;
            }
            else{
                x1=x0;
                f1=f0;
            }
            err=fabs((x2-x1)/x1);
        }while(err>E);
        printf("\nRoot: %0.4f", x0);
        printf("\nFunctional Value: %0.4f", f0);
        printf("\No of Iteration: %d", count);
    }
    getch();
    return 0;
}

No comments:

Post a Comment