All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Friday 13 February 2015

Bisection Method of Solving Equation

#include<iostream>
#include<math.h>
#include<iomanip>
#define bisect(a,b) (a+b)/2
#define err 0.0001
//Correction upto three places
using namespace std;
float f(float x)
{
return pow(x,4)-x-9; //Put Your Function here
}
int main()
{
int i=1,max;
float x,x1,a,b;
cout<<"Enter two initial approximate roots a,b : ";
cin>>a>>b;

cout<<"Enter max no of iterations: ";
cin>>max;
cout<<setprecision(10)<<"Iterations\tRoot\n";
x=bisect(a,b);
while(i<max)
{
cout<<i<<"\t\t"<<x<<endl;
if(f(a)*f(x)<0)
b=x;
else
a=x;
x1=bisect(a,b);
i++;
if(fabs(x1-x)<err)
{
cout<<"\nRoot after "<<i<<" iterations="<<setprecision(5)<<bisect(a,b)<<endl;
return 0;
}
x=x1;
}
cout<<"\nSolution Does not cover: "<<i<<" iteration not sufficient";
return 1;
}

No comments:

Post a Comment