- #1
Leonardo Machado
- 57
- 2
Hello guys ! May you help me with this little bisection program ?
For some reason it is not changing the value of the variables x0 and x1 after every loop :c
it just gets the root for its simple function.
Thanks for the help
For some reason it is not changing the value of the variables x0 and x1 after every loop :c
Code:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
double x, x0, x1, f, f0, f1;
x0=0; x1= 1000;
do {
x=(x0+x1)/2;
f= pow( x, 2)- 77*x+ 78;
f0= pow( x0, 2)- 77*x0+ 78;
f1= pow( x1, 2)- 77*x1+ 78;
if( f0>0 and f1<0 ){
if( f > 0){
x0=x;
}
else {
x1=x;
}
}
if( f0<0 and f1>0){
if( f > 0){
x1=x;
}
else{
x0=x;
}
}
if( (f0<0 and f1<0) or ( f0<0 and f1<0) ){
x0=x0/10;
x1=10*x1;
}
cout << "x0= " << x0 << " x1= " << x1 << " x= "<< x << " f0= " << f0 << " f1= " << f1 << endl;
}
while ( f>pow( 10, -2) or f<-pow( 10, -3));
cout << "resultado: " << x;
}
it just gets the root for its simple function.
Thanks for the help