Why Isn't My Bisection Program Updating Variables Correctly?

AI Thread Summary
The discussion revolves around troubleshooting a bisection program in C++. The primary issue is that the variables x0 and x1 are not updating as expected during each iteration of the loop. Participants suggest that the logic controlling the updates may be flawed, particularly pointing out a problematic conditional statement that checks the values of f0 and f1. There is also a recommendation to add print statements within the loop to monitor variable values for debugging purposes. Additionally, concerns are raised about the efficiency of using the pow() function for squaring numbers, with suggestions to replace it with simpler multiplication for better performance. The use of "and" and "or" as logical operators is discussed, with some expressing a preference for traditional symbols like "&&" and "||" for clarity and readability.
Leonardo Machado
Messages
56
Reaction score
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

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
 
Technology news on Phys.org
If you know it's not changing the values then you know the if statement that controls the change may have a problem.

The f0 and f1 variables are dependent on x0 and x1 so if they don't change then x0 and x1 can't change.

My suggestion is to place print statements inside your loop so you can see what the values are.
 
@Leonardo Machado, why do you have this line?
C:
if( (f0<0 and f1<0) or ( f0<0 and f1<0) ){
I think your logic might be flawed here.
This one near the bottom of your code is a bit mysterious.
C:
while ( f>pow( 10, -2) or f<-pow( 10, -3));
Your controlling expression for the do... while loop is when f > .01 or f < -.001. The lack of symmetry makes me think this is an error.

Also, I never do use pow() to calculate the square of a number, as in these lines:
C:
f= pow( x, 2)- 77*x+ 78;              
f0= pow( x0, 2)- 77*x0+ 78;
f1= pow( x1, 2)- 77*x1+ 78;

The first line would be much more efficient as f = x * x - 77 * x + 78; and similarly for the other two lines. The pow() function is extremely slow in comparison to multiplying a variable by itself.
 
Last edited:
Are "and" and "or" some new additions to the C language?
 
SlowThinker said:
Are "and" and "or" some new additions to the C language?
They are if you #include <iso646.h> for C code or #include <ciso646> for C++ code.
 
Mark44 said:
They are if you #include <iso646.h> for C code or #include <ciso646> for C++ code.
Or
Code:
#define and &&
#define or ||
Personally I abhor such conventions. I have seen it used to create a whole new language which is totally unreadable for people trying to debug the code.
 
Svein said:
Or
Code:
#define and &&
#define or ||
Which is how they are defined in the header I cited.
Svein said:
Personally I abhor such conventions. I have seen it used to create a whole new language which is totally unreadable for people trying to debug the code.
Yeah, I don't see much advantage in writing 'and' instead of '&&', or 'or' instead of '||'.
 
Mark44 said:
Yeah, I don't see much advantage in writing 'and' instead of '&&', or 'or' instead of '||'.
except for if you come from a pythonic background.
 

Similar threads

Replies
2
Views
4K
Replies
5
Views
3K
Replies
89
Views
5K
Replies
5
Views
2K
Replies
2
Views
3K
Replies
1
Views
2K
Back
Top