Explain Bisection Method with source code
BISECTION METHOD:
Algorithm:
a) assign
two initial values x1 and x2 and stopping criteria (or prescribed error) as E.
b) calculate
f(x1) and f(x2)
c) if
f(x1)*f(x2)>0 then x1 and x2 do not bracket the root, go to step (g)
otherwise continue
d) compute:
xm=(x1+x2)/2 and f(xm)
e) if
f(x1)*f(xm)<0 then
set x2=xm
else
set x1=xm
f) if
|x1-x2|/x2<E
then root= x1+x2/2
print the value of root
else
goto step (d)
g) STOP
Assumptions:
a) any
non-linear equation is defined as macro.
b) Estimated
precision of error is defined as E.
c) Required
data type is float.
d) The
program coded to solve the equation x3-5x+1=0
Source code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x)
x*x*x-5*x+1
#define E
0.00001
void main()
{
float x1,
x2, f1, f2, xm, f3, root;
clrscr();
printf(“enter
the initial values of x1 and x2:”);
scanf(“%f%f”,
&x1, &x2);
f1=f(x1);
f2=f(x2);
if((f1*f2)>0)
printf(“x1
and x2 do not bracket the root:”);
else
{
here:
xm=(x1+x2)/2;
f3=f(xm);
}
if((f1*f3)<0)
{
x2=xm;
}
else
{
x1=xm;
}
if(fabs(x1-x2)<E)
{
float root=xm;
printf(“the
root is %f”, root);
}
else
{
goto here;
}
getch();
}
0 comments:
Feel free to contact the admin for any suggestions and help.