Thursday, December 31, 2015

C Program code of Bisection method

 C Program code of Bisection method ( by Shrayashi Majumder)

         1.   Find the root of the equation(x3 - 4x-9=0) using bisection method.
Code:
#include<stdio.h>
#include<math.h>
double f(double x)
{
return x*x*x-4*x-9;
}
void main()
{
int i=0;
double mid,a=0.0,b=1.0,x=0.0,s=1.0,s1=1.0;
while(1)
{
s=f(a)*f(b);
s1=f(-a)*f(-b);
if(s<0.0 || s1<0.0)
        break;
else
{
        a=b;
        b++;
}
}
if(s1<0.0)
{
a=-a;
b=-b;
}
printf("Range %f to %f\n \n",a,b);
printf("\n          a        b        f(a)       f(b)    mid");
while(1)
{
mid=(a+b)/2;
printf("\nStep%d:  %f %f %f %f %f",i++,a,b,f(a),f(b),mid);
if(f(mid)==0.0)
        break;
if(fabs(a-b)<0.0001)
        break;
        if(f(a)*f(mid)<0.0)
                b=mid;
        else
                a=mid;
}
printf("\nx=%f",mid);

}
Output:
Range 2.000000 to 3.000000


          a        b        f(a)       f(b)    mid
Step0:  2.000000 3.000000 -9.000000 6.000000 2.500000
Step1:  2.500000 3.000000 -3.375000 6.000000 2.750000
Step2:  2.500000 2.750000 -3.375000 0.796875 2.625000
Step3:  2.625000 2.750000 -1.412109 0.796875 2.687500
Step4:  2.687500 2.750000 -0.339111 0.796875 2.718750
Step5:  2.687500 2.718750 -0.339111 0.220917 2.703125
Step6:  2.703125 2.718750 -0.061077 0.220917 2.710938
Step7:  2.703125 2.710938 -0.061077 0.079423 2.707031
Step8:  2.703125 2.707031 -0.061077 0.009049 2.705078
Step9:  2.705078 2.707031 -0.026045 0.009049 2.706055
Step10:  2.706055 2.707031 -0.008506 0.009049 2.706543
Step11:  2.706055 2.706543 -0.008506 0.000270 2.706299
Step12:  2.706299 2.706543 -0.004118 0.000270 2.706421
Step13:  2.706421 2.706543 -0.001924 0.000270 2.706482
Step14:  2.706482 2.706543 -0.000827 0.000270 2.706512
x=2.706512
2.  Find the root of equation using bisection method.
Code:
#include<stdio.h>
#include<math.h>
double f(float c3, float c2 , float c1 , float cn , double x)
{
double r;
r=c3*x*x*x+c2*x*x+c1*x+cn;
return r;
}
void main()
{
int i=0;
float c3,c2,c1,cn;
double mid,omid=100.0,a=0.0,b=1.0,s=1.0,s1=1.0,tol;
printf("Enter coefficients:");
scanf("%f%f%f%f",&c3,&c2,&c1,&cn);
printf("Enter Tolerance:");
scanf("%lf",&tol);
while(1)
{
s=f(c3,c2,c1,cn,a)*f(c3,c2,c1,cn,b);
s1=f(c3,c2,c1,cn,-a)*f(c3,c2,c1,cn,-b);
if(s<0.0 || s1<0.0)
        break;
else
{
        a=b;
        b++;
}
}
if(s1<0.0)
{
        a=-a;
        b=-b;
}
printf("Range %f to %f",a,b);
printf("\n    \ta        b        f(a)       f(b)    mid");
while(1)
{
        mid=(a+b)/2;
        printf("\nStep %d: %f %f %f %f %f",i++,a,b,f(c3,c2,c1,cn,a),f(c3,c2,c1,cn,b),mid);
        if(f(c3,c2,c1,cn,mid)==0.0)
                break;
        if(fabs(a-b)<tol)
                break;
        if(f(c3,c2,c1,cn,a)*f(c3,c2,c1,cn,mid)<0.0)
                b=mid;
        else
                a=mid;
        omid=mid;
}
printf("\nx=%f",mid);
}
Output:
Enter coefficients:1 0 -4 -9
Enter Tolerance:0.0001
Range 2.000000 to 3.000000
        a        b        f(a)       f(b)    mid
Step 0: 2.000000 3.000000 -9.000000 6.000000 2.500000
Step 1: 2.500000 3.000000 -3.375000 6.000000 2.750000
Step 2: 2.500000 2.750000 -3.375000 0.796875 2.625000
Step 3: 2.625000 2.750000 -1.412109 0.796875 2.687500
Step 4: 2.687500 2.750000 -0.339111 0.796875 2.718750
Step 5: 2.687500 2.718750 -0.339111 0.220917 2.703125
Step 6: 2.703125 2.718750 -0.061077 0.220917 2.710938
Step 7: 2.703125 2.710938 -0.061077 0.079423 2.707031
Step 8: 2.703125 2.707031 -0.061077 0.009049 2.705078
Step 9: 2.705078 2.707031 -0.026045 0.009049 2.706055
Step 10: 2.706055 2.707031 -0.008506 0.009049 2.706543
Step 11: 2.706055 2.706543 -0.008506 0.000270 2.706299
Step 12: 2.706299 2.706543 -0.004118 0.000270 2.706421
Step 13: 2.706421 2.706543 -0.001924 0.000270 2.706482
Step 14: 2.706482 2.706543 -0.000827 0.000270 2.706512

x=2.706512