CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2008
    Posts
    5

    Unhappy Problem with Bissection method

    Hello.
    First sorry for bad English.
    Can somebody help me with this:
    Using Bisection method find one real root for
    f(x)=x^9-x^7+2x^2-1 x E [0;1]

    I have to write a program in c++ witch founds me the root.

    The problem is that I am begginer in c++ and it is very difficult to me.

    this is what i can do sad.gif but it is very ugly and it doesn't work.can someone fix it and make it good arranged and beauty.Please help me.

    double F( double x){return x*x*x*x*x*x*x*x*x -x*x*x*x*x*x*x + 2*x*x - 1;}


    double L = -100 , R = 100;
    for(int i = 0; i < 1000; i++) // to make a 1000 steps
    {
    double Mid = (L + R) / 2;
    double current_value = F(Mid );
    if( current_value== 0 ) cout<<"root is: " <<Mid;
    if (current_value < 0 ) L = mid;
    else if (current_value> 0 ) R = mid;
    }


    I know that it may be very stupid and wrong but please help me

  2. #2
    Join Date
    Jul 2007
    Posts
    38

    Re: Problem with Bissection method

    I've noticed few things go wrong in your code.
    1. here you'll get return only an integer value. so there is a chance for you to miss the root. (In fact you will loose all the non-integer roots.
    Code:
    double Mid = (L + R) / 2;
    2. When compare a double value for equals to zero, you may have to set a tolerance (for eg: 0.000001) and check for the difference is less than that.
    There is a seperate thread explaining this issue.
    Code:
    if( current_value== 0

    Also I doubt your algorithm. what you are trying to implement seems correct only for a function continuously increasing along with x.
    Regards
    sris kanagasabai

  3. #3
    Join Date
    Apr 2008
    Posts
    5

    Re: Problem with Bissection method

    Well ... this is my code :

    #include <cstdlib>
    #include <iostream>



    int main(int argc, char *argv[])
    double F( double x){return x*x*x*x*x*x*x*x*x -x*x*x*x*x*x*x + 2*x*x - 1;}
    double L = -100 , R = 100;
    {for(int i = 0; i < 1000; i++) // to make a 1000 steps
    {
    double Mid = (L + R) / 2;
    double current_value = F(Mid );
    if( current_value== 0 ) cout<<"root is: " <<Mid;
    if (current_value < 0 ) L = mid;
    else if (current_value> 0 ) R = mid;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
    }


    I know that here are too many mistakes but again i am very beginner in programing.please make it work and to show the task and the result and to look nice.please.
    i know that may work could makes you laugh but ... this is what i can
    Help!

  4. #4
    Join Date
    Jul 2007
    Posts
    38

    Re: Problem with Bissection method

    you'll see a cleaner version of your code.

    but as I mentioned before this may not perform what you expect. if not, it is bcoz of your algorithm.
    I can see through your algorithm is not going to work for all pattern of fucntions. you need to come up with the accurate algorithm. or atleast it should work for a particular sub set of functions, that your one belongs to.

    this is nothing to do with programming.

    Code:
    #include <iostream>
    using namespace std;
    
    #include <math.h>
    
    double F(double x){
            double res = pow(x, 9) - pow(x, 7) + 2 * pow(x, 2) -1;
    
            return res;
    }
    
    void print_roots(){
            double low = -100, high = 100;
    
            for(int i = 0; i < 1000 ; ++i){
                    double mid = (float)(low + high)/2;
                    double val = F(mid);
    
                    cout << mid << ":" << val << endl;
    
                    if(val < 0.00001 && val > -0.00001){
                            cout << "Root is " << mid << endl;
                            return;
                    }
                    else if (val < 0 )
                            low = mid;
                    else
                            high = mid;
            }
    }
    
    int main(int argc, char **argv){
            print_roots();
            return 0;
    }
    Last edited by skanthaverl; April 4th, 2008 at 11:48 AM.
    Regards
    sris kanagasabai

  5. #5
    Join Date
    Jul 2007
    Posts
    38

    Re: Problem with Bissection method

    to get a more precise answer you can increse the precision we test in the if condition. eg: instead of 0.00001, use 0.0000001. remember you should not exceed the precision of double value.
    Regards
    sris kanagasabai

  6. #6
    Join Date
    Apr 2008
    Posts
    5

    Re: Problem with Bissection method

    thank you so much... i can't say how much grateful i am.
    if there is some other and better solution of this task i will be glad to see it.
    Again THANK YOU VERY MUCH.

  7. #7
    Join Date
    Apr 2008
    Posts
    5

    Red face Re: Problem with Bissection method

    i have another question.
    can it be made to add induction of different interval or different task.
    i mean to assign a task and interval and the program to give resolution.
    example : i start the program and it ask me to enter the task(bisection method) after entering it to ask me the intervals and when we do this to show the answer.
    thank you in advance.

  8. #8
    Join Date
    Apr 2008
    Posts
    5

    Bisection method c++ task

    Hello guys, I've got a problem and it would be wonderful if someone of you could help me. I'm talking about a bisection method task witch I must complete using c++
    f(x)=x^9-x^7+2x^2-1 for x [0;1]
    the code that I managed to write myself is here:


    #include <iostream>
    using namespace std;
    #include <math.h>

    double F(double x)
    { double res = pow(x, 9) - pow(x, 7) + 2 * pow(x, 2) -1;
    return res;
    }
    void print_roots(){

    double low, high;
    cout<<"low=";
    cin>>low;
    cout<<"high=";
    cin>>high;

    for(int i = 0; i < 1000; ++i){
    double mid = (float)(low + high)/2;
    double val = F(mid);

    cout << mid << ":" << val << endl;

    if(val < 0.00001 && val > -0.00001){
    cout << "Root is " << mid << endl;
    return;
    }
    else if (val < 0 )
    low = mid;
    else
    high = mid;
    }
    }

    int main(int argc, char **argv){
    print_roots();
    system("pause");
    return 0;
    }


    but it doesn't work as it should. I mean it should calculate any task of that kind, not only this one. It should also have input fields for all of the variables. I also have doubts about my program's correct calculations.
    I am sorry about my English, but I am still learning.
    Thank you in advance!

  9. #9
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Problem with Bissection method

    Hi rottweiler,

    Please don't post a new thread on the same topic, it's easier for everyone to keep track of all the information if it's in the same thread.

    One thing I don't like is this:

    if (val < 0 )
    low = mid;
    else
    high = mid;

    In the bissect method, you need to identify a sign-change to find the next interval. For example, let say you have the interval [low, high] and the point mid. In order to have a sign change, you need to have:

    F(low) * F(high) < 0

    To find the next interval (between [low, mid] or [mid, high]), you need to identify where is the sign change. The pseudo-code would be:

    if (F(low) * F(mid) < 0)
    high = mid
    elseif F(mid) * F(high) < 0
    low = mid
    else
    // mid is a root

    That way, your function can be anything, and the algorithm will work.

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured