// Program to find out the roots of equation, using Bisection Method.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
float f(float x) //Returning the value of f(x)
{
return(x*x*x-4*x-9);
}
void bisect(float *m, float a, float b, int *itr)
{
*m=(a+b)/2;
++(*itr);
printf(“Iteration no %3dx= %7.5fn”,*itr,*m) ;
}
main() //Main Program
{
clrscr();
int itr=0,maxitr;
float x,a,b,aerr,x1;
printf(“ntBISECTION METHOD TO CALCULATE ROOTS OF EQN.x*x*x-4*x-9nn”);
printf(“nnEnter the values of a, b, allowed error and maximum iterations==>n”);
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 Iterations, root=%6.4fn”,itr,x1);
delay(4000);
return 0;
}
x=x1;
}
while(itr<maxitr);
printf(“nSolution does not converge, Iterations not Sufficient”);
delay(4000);
return 1;
}
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.