0

I am making a program for solving questions of bi-section method and finding the value of one real root of the given equation. In evaluating the equation for different values of a given variable, firstly, we have to find the interval, in which the root will be found. So, in order to find the interval, we put different values (usually positive integers) of the variable (say x ) in the given equation and check the nearest two values for which the value of the equation is negative for one and positive for another. This is the interval of the root of the equation. After finding the possible interval we continue the procedure for finding one real root of the equation.

Now, I am stuck in finding the correct algorithm to determine the possible interval for the real root of the equation. I have already written the code for the user to provide the initial and final values of the interval manually and then the program checks the root of the equation based on the values given by the user. Then, the program successfully determines the root of the equation.

I have written a function for automatically finding the initial and final values of interval also. But, it is not working as it should be. I am unable to understand, what is going wrong with it and hence need help in the same.

/* This is my full code */
#include<bits/stdc++.h>
#include<conio.h>
#define EPSILON 0.00001

using namespace std;

class Bisection
{
    /*int noofcaparr, *coeffarr, *powerarr, eqn;
    double var_x, result, var_a, var_b, var_c;
    char *eqnprnt;*/

    public:
        int noofcaparr, *coeffarr, *powerarr, eqn;
        double var_x, result, var_a, var_b, var_c;
        char *eqnprnt;


        Bisection()
        {
            int noofcaparr = 0;
        }
        Bisection(const Bisection &obj)
        {
            this->noofcaparr = obj.noofcaparr;
            this->coeffarr = obj.coeffarr;
            this->powerarr = obj.powerarr;
            this->eqn = obj.eqn;
            this->var_x = obj.var_x;
            //this->result = obj.result;
            //this->var_a = obj.var_a;
            this->var_b = obj.var_b;
            this->var_c = obj.var_c;
            this->eqnprnt = obj.eqnprnt;
        }

        void geteqn();
        void showeqn();
        double setupeqn();
        void showresult();
        void getvariable();
        double roundoff(double &);
        Bisection bsmethodcomputn(Bisection &);
        Bisection check_intervalup();
        Bisection check_intervaldown();
        void setvariablea(Bisection &);
        void setvariableb(Bisection &);

};

void Bisection::getvariable()
{
    this->var_a = 0;
    cout<<"\n\n\n\t ENTER THE VALUE OF VARIABLE:  ";
    cin>>this->var_a;
}


void Bisection::setvariablea(Bisection &obj)
{
    this->var_a = this->var_b;
}

void Bisection::setvariableb(Bisection &obj)
{
    obj.var_a = obj.var_c;
}



void Bisection::geteqn()
{
    int c, i, n;
    system("cls");
    cout<<"\n\n\t\t How many terms do you have in your equation? ";
    cout<<"\n\t For Example:  x^3 - 4x - 9 = 0   , has '3' terms     and ";
    cout<<"\n\t               x^4 + x^3 - 7x^2 - x + 5 = 0   , has '5' terms";
    cout<<"\n\t Enter the number of terms present in your equation:  ";
    cin>>this->noofcaparr;
    n = this->noofcaparr-1;

    this->coeffarr =  new int[n];
    this->powerarr = new int[n];

    for(i=0, c=1; i<=n; i++, c++ )
    {
        cout<<endl<<endl<<"\t\t Please enter the "<<c<<" th/st/nd/rd highest degree of x:  ";
        cin>>this->powerarr[i];

        cout<<endl<<endl<<"\t\t Please enter the coefficient of "<<c<<" th/st/nd/rd highest degree of x (with sign -/+):  ";
        cin>>this->coeffarr[i];
    }
/*    cout<<"\n\n\n\t\t Enter the value of x:  ";
    cin>>this->var_a; */
    cout<<endl<<endl<<"\n\n\t Values Set!";
    getch();


}


void Bisection::showeqn()
{
    int i, n;
    n = this->noofcaparr-1;
    system("cls");
    cout<<endl<<endl<<"\t\t Your equation is:   ";

        for(i=0; i<=n; i++ )
        {

            if(this->powerarr[i]==0)
            {
                if(i==0)
                {
                    if(this->coeffarr[i]>= 0)
                    {
                        if(this->coeffarr[i]==0)
                        {
                            cout<<" +0 ";
                        }

                        else
                        {
                            if(this->coeffarr[i]==1)
                            {
                                cout<<" 1";
                            }

                            else
                            {
                                cout<<" "<<(this->coeffarr[i])<<" ";
                            }
                        }
                    }
                    else
                    {
                        if(this->coeffarr[i]== -1)
                        {
                            cout<<" -"<<"1";
                        }
                        else
                        {
                            cout<<" "<<(this->coeffarr[i])<<" ";
                        }
                    }
                }
                else
                {
                    if(this->coeffarr[i]>= 0)
                    {
                        if(this->coeffarr[i]==0)
                            cout<<" +0 ";

                        else
                            cout<<" +"<<(this->coeffarr[i])<<" ";
                    }
                    else
                    {
                        cout<<" "<<(this->coeffarr[i])<<" ";
                    }
                }
            }

            else
            {
                if(this->powerarr[i]==1)
                {
                    if(i==0)
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            if(this->coeffarr[i]==0)
                            {
                                cout<<" +0 ";
                            }
                            else
                            {
                                if(this->coeffarr[i]==1)
                                {
                                    cout<<"x";
                                }
                                else
                                {
                                    cout<<" +"<<(this->coeffarr[i])<<"x";
                                }
                            }
                        }
                        else
                        {
                            if(this->coeffarr[i]== -1)
                            {
                                cout<<" -"<<"x ";
                            }
                            else
                            {
                                cout<<(this->coeffarr[i])<<"x";
                            }
                        }
                    }
                    else
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            cout<<"+"<<(this->coeffarr[i])<<"x";
                        }
                        else
                        {
                            cout<<(this->coeffarr[i])<<"x";
                        }
                    }
                }
                else
                {
                    if(i==0)
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            if(this->coeffarr[i]==1)
                            {
                                cout<<"x^"<<this->powerarr[i]<<"  ";
                            }
                            else
                            {
                                if(this->coeffarr[i]==0)
                                {
                                    cout<<" +0 ";
                                }
                                else
                                {
                                    cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                                }
                            }

                        }
                        else
                        {
                            if(this->coeffarr[i]== -1)
                            {
                                cout<<" -"<<"x^"<<this->powerarr[i]<<"  ";
                            }
                            else
                            {
                                cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                            }
                        }
                    }
                    else
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            cout<<" +"<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                        }
                        else
                        {
                            cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                        }
                    }
                }
            }
        }
    cout<<" = 0  = f(x) {let}";
}

double Bisection::setupeqn()
{
    this->result = 0;
    double rslt, rndoff_res;
    rslt = this->result;
    int n = this->noofcaparr - 1;
    for (int i=0; i <= n; i++)
    {
        double pwr, cfr;
        double x;
        x = this->var_a;
        pwr= this->powerarr[i];
        cfr = this->coeffarr[i];
        rslt += (pow(x,pwr)*cfr);
        rndoff_res = this->roundoff(rslt);
        rslt = rndoff_res;
        //cout<<endl<<endl<<endl<<"\t Value of var_a: "<<x;
        //cout<<endl<<endl<<endl<<"\t Value of powerarr: "<<pwr;
        //cout<<endl<<endl<<endl<<"\t Value of coeffarr: "<<cfr;
        //cout<<"\n\n\t result +=  (pow(this->var_a, this->powerarr[i])* this->coeffarr[i]) = "<<rslt;
        this->result= rslt;
    }


    //cout<<endl<<endl<<endl<<"\t Value of result: "<<rslt;
    return (this->result);
}

void Bisection::showresult()
{

    cout<<endl<<endl<<"\t\t This is your result:  "<<this->result;

}

double Bisection::roundoff(double &res)
{
    // 37.66666 * 100 =3766.66
    // 3766.66 + .5 =37.6716    for rounding off value
    // then type cast to int so value is 3766
    // then divided by 100 so the value converted into 37.66
    ///double round_off_result = (int)(res * 100000 + .5);
    ///return (double)round_off_result / 100000;
    double round_off_result = res;
    setprecision(4);
    return round_off_result;
}

Bisection Bisection::bsmethodcomputn(Bisection &obj)
{
    double temp_c;
    Bisection obj2 = *this;

    cout<<"\n\n\t First value is = "<<this->result<<"\t\t Second value is = "<<obj.result;
    cout<<endl<<endl<<endl;
    //void bisection(double a, double b)

        if (this->result * obj.result >= 0)
        {

            cout << "\t \t You have not assumed right a and b\n";

            cout<<"\n\n\t First value is = "<<this->var_a<<"\t\t Second value is = "<<obj.var_a;
            //return;
        }



        else
        {

            cout<<"\n\n\t First value is = "<<this->var_a<<"\t\t Second value is = "<<obj.var_a;
            double a = this->var_a;
            double b = obj.var_a;
            //Bisection obj2 = *this ;

            obj2.var_a = a;
            obj2.setupeqn();
            double c = obj2.var_a;
            int counter = 1;

            while ((b-a) >= EPSILON)
            {
                // Find middle point
                temp_c = c;
                c = (a+b)/2;
                //c = obj2.roundoff(c);
                obj2.var_a = c;

                cout<<endl<<endl<<endl<<"\n\t\t\t\t "<<counter<<").";
                cout<<endl<<endl<<endl<<"\t Value of c: "<<obj2.var_a;


                obj2.setupeqn();

                cout<<endl<<endl<<endl<<"\t Value of result:  "<<obj2.result;

                // Check if middle point is root
                if (obj2.result == 0.0001)
                    break;

                else
                {
                    if(temp_c==c)
                        break;
                // Decide the side to repeat the steps
                    else
                    {
                        if (obj2.result* this->result < 0)
                        {
                            b = c;
                        }
                        else
                            a = c;
                    }
                }

                counter++;
            }

            cout << "\n\n\n\n\t\t\t\t *** The value of root is : " << c<<" ***"<<endl<<endl<<endl;
            getch();

        }

}

Bisection Bisection::check_intervalup()
{
    double a;
    int c;
    for(a=0.00, c=1; this->result<0; a+=0.25, c++)

    {
        this->var_a = a;
        this->setupeqn();

        /*if(this->result > -1.0 && this->result < 0.00 )
        {
            break;
        } */

        cout<<"\n\t"<<c;
        cout<<"\n\t Value of result is :  "<<this->result<<"\t\t on value of variable:  "<<a;
    }
        this->var_b = a;
//    return this->var_b;
    getch();
}

Bisection Bisection::check_intervaldown()
{
    this->var_a = 1;
    int c;
    double a = this->var_a, resultfi = this->result;
    for(a=0.00, c=1; this->result> 0; a += 0.25, c++)
    {
        this->var_a = a;
        this->setupeqn();

        /* if(this->result < 1.00 && this->result >0)
            break; */
        cout<<"\n\t"<<c;
        cout<<"\n\t Value of result is :  "<<this->result<<"\t\t on value of variable:  "<<a;
    } //   return this->var_c;
        this->var_c=a;
    getch();
}


int main()
{
    Bisection a;

    a.geteqn();
    a.showeqn();

    char choice;

    do
    {
    Bisection b = a;
    a.check_intervalup();
    cout<<endl<<endl<<endl<<a.var_b;
    b.check_intervaldown();
    cout<<endl<<endl<<endl<<b.var_c;
    a.setvariablea(b);
    a.setvariableb(b);
    cout<<endl<<endl<<endl<<a.var_a<<"\t\t"<<b.var_a;
    //a.getvariable();
    a.setupeqn();
    cout<<"\n\n\t\t Equation is set-up";
    a.showresult();
    //b.getvariable();
    b.setupeqn();
    b.showresult();

    a.bsmethodcomputn(b);

    cout<<"\n\n\n\t Do you want to continue? (Y/N)";
    cin>>choice;
    }while(choice=='Y'||choice=='y');
    getch();
    return(0);
}

I might have some problems in these pieces of the code:

Bisection Bisection::check_intervalup()
{
    double a;
    int c;
    for(a=0.00, c=1; this->result<0; a+=0.25, c++)

    {
        this->var_a = a;
        this->setupeqn();

        /*if(this->result > -1.0 && this->result < 0.00 )
        {
            break;
        } */

        cout<<"\n\t"<<c;
        cout<<"\n\t Value of result is :  "<<this->result<<"\t\t on value of variable:  "<<a;
    }
        this->var_b = a;
//    return this->var_b;
    getch();
}

Bisection Bisection::check_intervaldown()
{
    this->var_a = 1;
    int c;
    double a = this->var_a, resultfi = this->result;
    for(a=0.00, c=1; this->result> 0; a += 0.25, c++)
    {
        this->var_a = a;
        this->setupeqn();

        /* if(this->result < 1.00 && this->result >0)
            break; */
        cout<<"\n\t"<<c;
        cout<<"\n\t Value of result is :  "<<this->result<<"\t\t on value of variable:  "<<a;
    } //   return this->var_c;
        this->var_c=a;
    getch();
}


void Bisection::setvariablea(Bisection &obj)
{
    this->var_a = this->var_b;
}

void Bisection::setvariableb(Bisection &obj)
{
    obj.var_a = obj.var_c;
}

I am expecting from my program that when I or any other user provides it the required equation then, it should automatically check the values (values of this->var_a ) of the given variable (say x ) for which it has maximum value ( value of this->result ) in negative(say -0.0063, -0.500, -1.0032 etc. but not less than -1.20 e.g. -2.0, -3.0, -1.79) but should be less than '0' while putting the values of the given variable as 0.00, 0.25, 0.50, 0.75, 1.00, 1.25, 1.50, 1.75 and so on... And it should store/assign that value of x in this->var_a or this->var_b, or this->var_c

Similarly, it should check for the value of this->var_a for which equation possesses minimum positive value of this->result (>0)

I've tried writing the code but I cannot understand what is the problem and why my program cannot find the correct interval automatically. The error/mistake could be very small or silly but I still couldn't identify it. That's why your help will be very much appreciated. Thank You So Much in Advance!

  • 1
    Normally I am happy to help. But this code has so many syntax and semantic and design errors that I cannot do. Can you please run the compiler over your code and switch on C++ (Not C) and enable all warnings and messages. Then remove the syntax errors and warnings, then edit you code and post it again. This is useless – A M Jul 13 '19 at 08:28
  • @ArminMontigny I'd happily do it but, my compiler isn't showing me any warning or messages. I am using Code Blocks with GCC compiler. May you please give any further information to know how can I do it. I accept the fact that my program is full of foolishness or mistakes as I am just a newbie and don't know much about making efficient programs. – Naved THE Sheikh Jul 13 '19 at 08:42

0 Answers0