- #1
ggeo1
- 63
- 0
Hello,
I wrote a code for my problem but i can't find the mistake.It doesn't give me the right answer.
We must find the root using bisection method.
I found with mathematica that the solution is x= -3.18306.
The function values are x (-4,-3).
Any help is welcome!
I wrote a code for my problem but i can't find the mistake.It doesn't give me the right answer.
We must find the root using bisection method.
I found with mathematica that the solution is x= -3.18306.
The function values are x (-4,-3).
Any help is welcome!
Code:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
double function(double); // declare of function
int main()
{
double x1,x2,x3,error;
double eps=pow(10,-6); //my error tolerance
cout << "Give x1 : "<<endl;
cin >>x1;
cout << "Give x2 : "<<endl;
cin >>x2;
error = abs((x1-x2)/x1); //compute relative error
int i=1;
while ((error > eps) || (function(x3))!=0) // execute the loop as far as these conditions exist
{
x3=(x1+x2)/2.0; // midpoint
if ((function(x3))*(function(x1)) <0) //condition for bisection method
x2=x3;
else
x1=x3;
i++;
cout << " The function(root) is : " << function(x3) << "\t and the root is : " << x3<<endl;
}
return 0;
}
double function(double x) // my function
{
double y;
y =exp(x)-sin((3.14159265*x)/180.0);
return y;
}