1. Write a program to demonstrate Bisection Method.

            
    /* Program to execute Bisection method */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    double f(float x)
    {
        double val=0.0;
        val=x*x*x-x*x-x-3;
        return val;
    }
    void main()
    {
        int maxitr,count=0;
        float x0,x1,x2,aerr;
        double err,y0,y1,y2;
        printf("\nEnter initial guesses, no. of iterations and maximum allowed error\n");
        scanf("%f %f %d %f",&x0,&x1,&maxitr,&aerr);
        while(count<maxitr) 
            {
                y0=f(x0);
            y1=f(x1);
            if(y0==0) 
                {
                    printf("root is %f",x0);
                    return;
                } 
            else if(y1 ==0)
            {
                printf("Root is %f",x1);
                return;    
            } 
            else if(y0*y1>0) 
            {
                printf("Unsuitable starting values");
                return;
            } 
            else 
            {
                x2=(x0+x1)/2;
                y2=f(x2);
                if(y0*y2<0)
                    x1=x2;    
                else
                    x0=x2;
                err=fabs(x1-x0);    
                if(err<aerr) 
                {
                    count ++;
                    printf("After %d itearations root =%f\n",count,x2);
                    return;
                }  
                    count++;    
                    printf("Iteration no. %d: x= %f\n",count,x2);     
            }    
        }   
        printf("\nSolution does not converge, iterations not sufficient");
        return;
    }

        

Output

    Enter initial guesses, no. of iterations and maximum allowed error
    2 3 14 0.001
    Iteration no. 1: x= 2.500000
    Iteration no. 2: x= 2.250000
    Iteration no. 3: x= 2.125000
    Iteration no. 4: x= 2.187500
    Iteration no. 5: x= 2.156250
    Iteration no. 6: x= 2.140625
    Iteration no. 7: x= 2.132812
    Iteration no. 8: x= 2.128906
    Iteration no. 9: x= 2.130859
    After 10 itearations root =2.129883