Vous êtes sur la page 1sur 5

1

BISECTION METHOD

Aim
To find the root of a polynomial equation using bisection method.

Theory
In this method of successive bisection we begin the iterative cycle by picking two
trial points which enclose the root. The two points are x0 and x1 enclose a root if f(x0) and f(x1)
are of opposite signs. We now bisect the interval (x0, x1) and denote the mid-point by x2.
X2 = ( x1 + x0 ) / 2
If f(x0) = 0 then we have a root. However if f(x2) > 0 then the root is between x0 and x2. We
now replace x1 by x2 and search for the root in this new interval. We again calculate f(x2) at the
mid-point of this new interval. If f(x2) < 0, then the root is between x2 and x1. We now replace
x0 by x1 and again bisect the interval.

Algorithm
Step 1

: Start

Step 2 : Read x0 ,x1 ,e.


Step 3

: y0 f(x0)

Step 4

: y1 f(x1)

Step 5

: if (sign (y0) = (y1)) then


Begin Write starting values are unsuitable.
Write x0, x1, y0, y1.
Stops end
: While | (x1 x0 ) / x1 | > e do
begin

Step 6
Step 7

: x2 ( x0 + x1 ) / 2

Step 8

: y2 f(x2)

Step 9

:ii+1

Step 10 : if (sign (y0) = (y2)) then


x0 x2 else x1 x2
end
Step 11 : Write solution converges to root
Step 12 : Write number of iterations = i
Step 13 : write x2 and y2.
Step 14 : Stop

Flowchart

Source Code
#include<math.h>
#include<conio.h>
#include<iostream.h>
class bisection
{
private:
int i,n,m;
float d[20],a,b,c,h,g,x,y1,y2,y3;
public:
bisection()
{
i=0;
n=0;
m=0;
a=0;
b=0;
c=0;
h=0;
g=0;
x=0;
y1=0;
y2=0;
y3=0;
}
void getfunction();
float calculate(float x);
void solve();
};
void bisection::getfunction()
{
cout<<"Enter the order of the equation:";
cin>>n;
cout<<"\n Enter the coefficients:";
for(i=0;i<=n;i++)
{

4
cin>>d[i];
}
cout<<"\n Enter the initial check points:";
cin>>a>>c;
}
float bisection::calculate(float x)
{
h=0;
g=0;
m=n;
for(i=0;i<=n;i++)
{
g=d[i]*pow(x,m);
h=h+g;
m=m-1;
}
return(h);
}
void bisection::solve()
{
y1=calculate(a);
y2=calculate(c);
if(y1*y2>0)
{
cout<<"Your choice of check points are not appropriable"<<endl;
cout<<"Please press ENTER to quit and try once more";
}
else
{
cout<<endl;
cout<<a<<"\t\t\t\t\t\t\t" <<y1<<endl;
cout<<c<<"\t\t\t\t\t\t\t" <<y2;
b=(a+c)/2;
y3=calculate(b);
while(fabs(y3)>0.00001)
{
cout<<endl;
cout<<b<<"\t\t\t\t\t\t\t" <<y3;
if(y1*y3>0)
a=b;
else
c=b;
b=(a+c)/2;
y3=calculate(b);
}
}

5
}
void main()
{
clrscr();
bisection obj;
obj.getfunction();
cout<<"x"<<"\t\t\t\t\t\t\t "<<"y"<<endl;
obj.solve();
getch();
}

Output
Enter the order of the equation: 2
Enter the coefficients: 1 0 -25
Enter the initial check points: 2 7
x
2
7
4.5
5.75
5.125
4.8125
4.96875
5.046875
5.007813
4.988281
4.998047
5.00293
5.000488
4.999268
4.999878
5.000183
5.000031
4.999954
4.999992
5.000011
5.000002
4.999997

y
-21
24
-4.75
8.0625
1.265625
-1.839844
-0.311523
0.470947
0.078186
-0.11705
-0.019527
0.029305
0.004883
-0.007324
-0.001221
0.001831
0.000305
-0.000458
-7.629395e-05
0.000114
1.907349e-05
-2.861023e-05

Vous aimerez peut-être aussi