Professional Documents
Culture Documents
Dipak Chaudhary
(Lecturer, OCEM)
Lab1.
BASIC THEORY
The bisection method is a root-finding method that repeatedly bisects an interval and then selects
a subinterval in which a root must lie for further processing. It is a very simple and robust
method, but it is also relatively slow. Because of this, it is often used to obtain a rough
approximation to a solution which is then used as a starting point for more rapidly converging
methods. The method is also called the interval halving method, the binary search method, or
the dichotomy method.
Figure 1 A few steps of the bisection method applied over the starting range [a1, b1]. The bigger dot is the root of the function.
The method is applicable for numerically solving the equation f(x) = 0 for the real variable x,
where f is a continuous function defined on an interval [a, b] and where f(a) and f(b) have
opposite signs. In this case a and b are said to bracket a root since, by the intermediate value
theorem, the continuous function f must have at least one root in the interval (a, b).
At each step the method divides the interval in two by computing the midpoint x = (a+b) / 2 of
the interval and the value of the function f(x) at that point. Unless x is itself a root (which is very
unlikely, but possible) there are now only two possibilities: either f(a) and f(x) have opposite
signs and bracket a root, or f(x) and f(b) have opposite signs and bracket a root. The method
selects the subinterval that is guaranteed to be a bracket as the new interval to be used in the next
step. In this way an interval that contains a zero of f is reduced in width by 50% at each step.
The process is continued until the interval is sufficiently small.
Explicitly, if f(a) and f(x) have opposite signs, then the method sets x as the new value for b, and
if f(b) and f(x) have opposite signs then the method sets x as the new a. (If f(x)=0 then x may be
taken as the solution and the process stops.) In both cases, the new f(a) and f(b) have opposite
signs, so the method is applicable to this smaller interval.
1
Er. Dipak Chaudhary
(Lecturer, OCEM)
ALGORITHM
1. Start
2. Read a, b, e
*Here a and b are initial guesses
e is the absolute error i.e. the desired degree of accuracy*
3. If (f(a)*f(b) > 0, then display initial guesses are wrong and Goto (2).
Otherwise continue.
4. Calculate x = (a + b)/2
5. If (f(x) > 0), then b = x
Else a = x and Goto (4).
6. If ( [ (b – a)/x ] > e ), then display x and Goto (7).
* Here [ ] refers to the modulus sign. *
Else Goto (4)
*Now the loop continues with new values.*
7. Stop
FLOW CHART
2
Er. Dipak Chaudhary
(Lecturer, OCEM)
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
# define f(x) x*x*x-2*x-5
void main()
{
float a,b,x;
int i=0;
printf("Enter the a and b:\n");
scanf("%f%f",&a,&b);
if(f(a)*f(b)>=0)
{
printf("The interval is wrong");
}
do
{
x=(a+b)/2;
printf("iteration:%d\ta=%f\tb=%f\tX%d=%f\tf(x%d)=%f\n",i,a,b,i,x,i,f(x));
if(f(x)>0)
b=x;
else
{a=x;}
i++;
} while(fabs(b-a)>0.001);
printf("\n\nThe root is %f\n",x);
getch();
}
Work out for the root of equation (a) x3 = 4x-9; (b) x4-8x2+15=0; (c) x3+x2+x+7=0
DISCUSSION:
3
Er. Dipak Chaudhary
(Lecturer, OCEM)
1. Start
2. Get values of x0, x1 and e
*Here x0 and x1 are the two initial guesses
e is the stopping criteria, absolute error or the desired degree of accuracy*
3. Compute f(x0) and f(x1)
4. Compute x2 = [x0*f(x1) – x1*f(x0)] / [f(x1) – f(x0)]
5. Test for accuracy of x2
If [ (x2 – x1)/x2 ] > e, *Here [ ] is used as modulus sign*
then assign x0 = x1 and x1 = x2
goto step 4
Else,
goto step 6
6. Display the required root as x2.
7. Stop
4
Er. Dipak Chaudhary
(Lecturer, OCEM)
Source Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
float x1,x2,x3,f1,f2,t;
scanf("%f",&x1);
scanf("%f",&x2);
printf("\n______________________________________________\n");
printf("\n______________________________________________\n");
do
f1=F(x1);
f2=F(x2);
x3=x2-((f2*(x2-x1))/(f2-f1));
printf("\n%f %f %f %f %f",x1,x2,x3,f1,f2);
x1=x2;
x2=x3;
}while(fabs(x2-x1)>ESP);
printf("\n______________________________________________\n");
printf("\n\nApp.root = %f",x3);
getch();