Solution of Non- Linear Equation in single variable using the method of successive bisection. - Computer Programming

# Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

## Tuesday, December 13, 2011

AIM:-
Solution of Non- Linear Equation in single variable using the method of successive bisection.

ALGORITHM:-
Bisection method.

1.  Decide initial values of a & b and stopping criterion, E.
2.  Compute f1 =f(a) & f2 =f(b)
3.   if  f1 * f2 >0, a & b do not have any root and go to step 7; otherwise continue.
4.  Compute *x=(a+b) /2 and compute f0 =f(*x)
5.  If f1 * f0 <0 then
Set b=*x
Else
Set a= *x
Set f1 = f0
6. If absolute value of (b-a)/ b is less than error E, then
root=(a+b)/2
write the value of root
go to step 7
else
go to step 4
7. stop

PROGRAM
/* Bisection method. */

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-4*x-9);
}
void bisect(float *x,float a,float b ,int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("iteration no. %3d x = %7.5f\n",*itr,*x);
}
main()
{
int itr=0,maxitr;
float x,a,b,aerr,x1;
clrscr();
printf("enter the value of a,b,aloowed error,maximum iteration \n");

3

scanf("%f%f%f%d",&a,&b,&aerr,&maxitr);
bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else a=x;
bisect(&x1,a,b,&itr);
if (fabs(x1-x)<aerr)
{
printf("after %d iteration ,root =%6.4f\n",itr,x1);
getch();
return 0;
}
x=x1;
}
While (itr<maxtir);
Printf(“solution does not coverage,” “iteration not sufficient”);
return1;
}
Result:-
Enter the value of a, b, allowed error, maximum iterations
…………………
Iteration No.  1 x=
Iteration No.  2 x=
-----------------------
-----------------------
After iteration, root=