Hey everybody.. I have some problem here.
This is the question: Find a root of the equation on the interval [4,5] by using the bisection method. What happens on the interval [ 1 , 2] ?
I wrote the code and everything seems fine with the interval [4,5] but for interval [1,2] the cout supposed to be no convergence but the code cout the root. Please help me. Thank you in advance.:)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f( double);
int main ( int argc, char *argv[])
{
int kmax;
double a,b,eps;
cout <<" Input a,b, eps, kmax\n ";
cin>> a >> b >> eps >> kmax;//use big kmax , eps 0.5e-6
cout << " The input data are\n";
cout << "a= "<< a << "b= "<< b;
cout << "eps= "<< eps << "kmax= "<< kmax<< endl;
cout<< " The results are\n";
cout<< "k x f(x) a b\n";
int k=1;
double x=0.5*(a+b);//first bisection
while (( k<=kmax) && ((b-a)>eps))
{
cout << setw(2)<< k << setprecision(5)<< fixed<< setw(8)<< a<< setw(8)<< b << setprecision(6) <<setw(10)<<x<< setw(12)<<f(x) << endl;
if (f(x) <0)
a=x;
else
b=x;
x= 0.5*(a+b);
k++;
}
if (k>kmax)
cout << "no convergence";
else
cout << " \nThe root = "<< x<< endl;
return 0;
}
double f(double x){
return (tan(x)-x);
}