Simple bisection program - solved

In summary: I know that python uses 'and' and 'or' for boolean operations. So it might be more natural for some people.except for if you come from a pythonic background. I know that python uses 'and' and 'or' for boolean operations. So it might be more natural for some people.Yes, that's true. It could be useful for people who are transitioning from Python to C/C++.Yes, that's true. It could be useful for people who are transitioning from Python to C/C++.
  • #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

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
  • #2
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.
 
  • #3
@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:
  • #4
Are "and" and "or" some new additions to the C language?
 
  • #5
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.
 
  • #6
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.
 
  • #7
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 '||'.
 
  • #8
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.
 

1. What is a simple bisection program?

A simple bisection program is a computer program that uses the bisection method, also known as the binary search method, to find the root of a function. This method involves repeatedly dividing an interval in half and checking if the root falls within the divided interval.

2. How does the bisection method work?

The bisection method works by first defining an interval on the x-axis where the root of the function is expected to lie. The program then divides this interval in half and checks if the root falls within the divided interval. If it does, then the divided interval becomes the new interval and the process is repeated until the root is found with a desired level of accuracy.

3. What are the advantages of using a bisection program?

One advantage of using a bisection program is that it is a simple and straightforward way to find the root of a function. It also guarantees convergence to a solution, as long as the function is continuous and the initial interval contains the root. Additionally, the bisection method is relatively easy to implement and can handle a wide range of functions.

4. Can the bisection method be used for finding multiple roots?

No, the bisection method is only used for finding a single root of a function. If a function has multiple roots, then the bisection method would only be able to find one of them. Other methods, such as the Newton-Raphson method, can be used to find multiple roots.

5. What are some common applications of the bisection method?

The bisection method is commonly used in various fields of science and engineering, such as in optimization problems, root finding in polynomial equations, and finding the eigenvalues of a matrix. It can also be used in financial calculations, such as finding the yield on a bond or determining the value of an option contract.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
3K
  • Linear and Abstract Algebra
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
3
Views
3K
  • Programming and Computer Science
3
Replies
89
Views
4K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top