Why Isn't My Bisection Program Updating Variables Correctly?

Click For Summary

Discussion Overview

The discussion revolves around a bisection program written in C++ that is not updating the variables x0 and x1 correctly during its execution. Participants explore potential issues in the code, including logical errors and inefficiencies in the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the issue with x0 and x1 not changing may be related to the if statements controlling their updates.
  • Another participant questions the logic of a specific conditional statement in the code, indicating it might be flawed.
  • Concerns are raised about the efficiency of using the pow() function for squaring numbers, with a recommendation to use multiplication instead.
  • Several participants discuss the use of "and" and "or" in the code, questioning whether these are standard additions to the C language or defined through specific headers.
  • There is a sentiment expressed against using "and" and "or" in place of "&&" and "||", with one participant noting it can lead to unreadable code.

Areas of Agreement / Disagreement

Participants express differing views on the use of "and" and "or" in the code, with some supporting their use and others criticizing it. The discussion remains unresolved regarding the best practices for coding in this context.

Contextual Notes

There are unresolved questions about the logical structure of the program and the implications of using specific coding conventions. The efficiency of mathematical operations is also a point of contention.

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;
           
           
        }
   
        count << "x0= " << x0 << "    x1= " << x1 << "    x= "<< x << "    f0= " << f0 << "    f1= " << f1 << endl;
       
    }
   
    while ( f>pow( 10, -2) or f<-pow( 10, -3));   
   
    count << "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 36 ·
2
Replies
36
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K