I have 2 main if statements inside this for loop (lines 7 and 56). I'm trying to get so that if after all the iterations are complete, i.e. j = 51 the first and second if statements are not satisfied, then it will print out an error message. I have currently tried to do this with another if else statement, however at the moment this is getting printed out no matter what.

Can anyone please explain how to do this?

for (double j = -50; j <= 51; j=j+0.1)
    {
        f(B, j, p, degree);

        if ( f(B, j, p, degree) <= 0 && f(B, j+0.1, p, degree) > 0 )
        {
            cout << "\nA root is between the interval (" << j <<","<< j+1 <<")"<<endl;

            left_endpoint = j;

            x1 = left_endpoint;

            f1 = f(B, x1, p, degree);

            right_endpoint = j+1;

            xmid = right_endpoint;

            fmid = f(B, xmid, p, degree);

            width = (right_endpoint - left_endpoint);

            for (int i = 1; i <= max; i++)
            {
                x2 = (x1 + xmid) / 2.0;
                cout << "The next midpoint, approximation is " <<fixed<<setprecision(7)<< x2 << endl;
                f2 = f(B, x2, p, degree);

                if (f1 * f2 <= 0.0) // root is in left half interval
                {
                    current_width = (x2 - x1) / 2.0;
                    fmid = f2;
                    xmid = x2;
                }

                else // root is in right half interval
                {
                    current_width = (xmid - x2) / 2.0;
                    f1 = f2;
                    x1 = x2;
                }

                if (current_width < epsilon)
                {
                    cout << "\nA root at x = " << x2 << " was found "
                    << "in " << i << " iterations" << endl;
                    f2 = f(B, x2, p, degree);
                    cout << "The value of the function at the root is " << f2 << endl;
                    break;
                }
            }
        }

        else if ( f(B, j, p, degree) >= 0 && f(B, j+0.1, p, degree) < 0 )
        {
            cout << "\nA root is between the interval (" << j <<","<< j+1 <<")"<<endl;

            left_endpoint = j;

            x1 = left_endpoint;

            f1 = f(B, x1, p, degree);

            right_endpoint = j+1;

            xmid = right_endpoint;

            fmid = f(B, xmid, p, degree);

            width = (right_endpoint - left_endpoint);

            for (int i = 1; i <= max; i++)
            {
                x2 = (x1 + xmid) / 2.0;
                cout << "The next midpoint, approximation is " <<fixed<<setprecision(7)<< x2 << endl;
                f2 = f(B, x2, p, degree);

                if (f1 * f2 <= 0.0) // root is in left half interval
                {
                    current_width = (x2 - x1) / 2.0;
                    fmid = f2;
                    xmid = x2;
                }

                else // root is in right half interval
                {
                    current_width = (xmid - x2) / 2.0;
                    f1 = f2;
                    x1 = x2;
                }

                if (current_width < epsilon)
                {
                    cout << "\nA root at x = " << x2 << " was found "
                    << "in " << i << " iterations" << endl;
                    f2 = f(B, x2, p, degree);
                    cout << "The value of the function at the root is " << f2 << endl;
                    break;
                }
            }
        }

        else if (j = 51)
        {
        cout << "\nThe search for a root has failed. The polynomial you have entered"
        << "\ncontains one or more complex roots." << endl;
        //break;
        }

    }

Recommended Answers

All 9 Replies

How does the declaration looks like for f() ?

line 105: >> else if (j = 51)

You are using the wrong operator. Use boolean == instead of assignment = operator.

Thank you for the replies.

Ancient Dragon, I have changed it as you said, however it is still not working.

I'm testing it with the polynomial x^ + 9 = 0, which has complex roots, hence the error message needs to be outputted. However nothing happens at the moment.

I have included all the code to perhaps make it clearer.

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

//Declare functions
void poly(vector<double> A, vector<double>& B, double x, double& p, double& px, double& pxx, int& degree);
void bisection(vector<double> B, double left_endpoint, double right_endpoint, double epsilon, double p, int max, int degree);
double f(vector<double>, double, double, int);
double fx(vector<double>, double, double, int);

//---------------------------------------------------------------------------------------

int main()
{
    vector<double> A;                  // vector of coefficients a^degree...a^0
    vector<double> B;                  // backup copy of vector A
    int degree;                        // highest degree
    double x;                          // value of x
    double p;                          // p(x)
    double px;                         // p'(x)
    double pxx;                        // p''(x)

    int max = 0;                       // maximum number of iterations
    double left_endpoint = 0.0;        // left endpoint of interval
    double right_endpoint = 0.0;       // right endpoint of interval
    double epsilon = 0.000001;         // convergence criterion

    poly(A, B, x, p, px, pxx, degree);
    bisection(B, left_endpoint, right_endpoint, epsilon, p, max, degree);

    return 0;
}

//---------------------------------------------------------------------------------------

void poly(vector<double> A, vector<double>& B, double x, double& p, double& px, double& pxx, int& degree)
{
    cout << "For polynomial p(x) = a0 + a1x + a2x^2 + ... + anx^n" << endl;
    cout << "Enter the polynomial degree, n" << endl;
    cout << "Example:  2 for a quadratic, 3 for a cubic..." << endl;
    cout << "Degree: ";
    cin >> degree;

    A.resize (degree + 1);
    B.resize (degree + 1);

    for (int i = degree; i >= 0; i--)
    {
        cout << "Enter coefficient a" << i << ": ";
        cin >> A[i];
    }

    for (int i = degree; i >= 0; i--)
    {
        B[i] = A[i];
    }

    cout << "Enter the value of x for which to solve: ";
    cin >> x;

    //Calculations for p(x)

    p = A[degree];
    for (int i = (degree - 1); i >= 0; i--)
    {
        p = p*x;
        p = p+A[i];
    }

    //First differentiation on the coefficients
    for (int i = 1; i <= degree; i++)
    {
        A[i - 1] = i * A[i];
    }

    //Horner's Method - calculations for p'(x)
    px = A[degree - 1];
    for (int i = (degree - 2); i >= 0; i--)
    {
        px = px*x;
        px = px+A[i];
    }

    //Second differentiation on the coefficients
    for (int i = 1; i <= degree-1 ; i++)
    {
        A[i - 1] = i * A[i];
    }

    //Horner's Method - calculations for p''(x)
    pxx = A[degree - 2];
    for (int i = (degree - 3); i >= 0; i--)
    {
        pxx = pxx*x;
        pxx = pxx+A[i];
    }

    cout << "  p\(" << x << ") = " << p << endl;
    cout << " p\'(" << x << ") = " << px << endl;
    cout << "p\''(" << x << ") = " << pxx << endl;
}

//---------------------------------------------------------------------------------------

void bisection(vector<double> B, double left_endpoint, double right_endpoint, double epsilon, double p, int max, int degree)
{
    double x1 = 0.0; // left endpoint of current interval
    double x2 = 0.0; // right endpoint of current interval
    double xmid = 0.0; // computed midpoint of current interval
    double f1 = 0.0; // function evaluated at left endpoint of current interval
    double f2 = 0.0; // function evaluated at right endpoint of current interval
    double fmid = 0.0;; // function evaluated at computed midpoint of current interval
    double width = 0.0; // width of original interval (b - a)
    double current_width = 0.0; // width of current interval (xmid - x1)

    cout << "Enter the maximum number of iterations allowed: ";
    cin >> max;

    for (double j = -50; j <= 51; j=j+0.1)
    {
        f(B, j, p, degree);

        if ( f(B, j, p, degree) <= 0 && f(B, j+0.1, p, degree) > 0 )
        {
            cout << "\nA root is between the interval (" << j <<","<< j+1 <<")"<<endl;

            left_endpoint = j;

            x1 = left_endpoint;

            f1 = f(B, x1, p, degree);

            right_endpoint = j+1;

            xmid = right_endpoint;

            fmid = f(B, xmid, p, degree);

            width = (right_endpoint - left_endpoint);

            for (int i = 1; i <= max; i++)
            {
                x2 = (x1 + xmid) / 2.0;
                cout << "The next midpoint, approximation is " <<fixed<<setprecision(7)<< x2 << endl;
                f2 = f(B, x2, p, degree);

                if (f1 * f2 <= 0.0) // root is in left half interval
                {
                    current_width = (x2 - x1) / 2.0;
                    fmid = f2;
                    xmid = x2;
                }

                else // root is in right half interval
                {
                    current_width = (xmid - x2) / 2.0;
                    f1 = f2;
                    x1 = x2;
                }

                if (current_width < epsilon)
                {
                    cout << "\nA root at x = " << x2 << " was found "
                    << "in " << i << " iterations" << endl;
                    f2 = f(B, x2, p, degree);
                    cout << "The value of the function at the root is " << f2 << endl;
                    break;
                }
            }
        }

        else if ( f(B, j, p, degree) >= 0 && f(B, j+0.1, p, degree) < 0 )
        {
            cout << "\nA root is between the interval (" << j <<","<< j+1 <<")"<<endl;

            left_endpoint = j;

            x1 = left_endpoint;

            f1 = f(B, x1, p, degree);

            right_endpoint = j+1;

            xmid = right_endpoint;

            fmid = f(B, xmid, p, degree);

            width = (right_endpoint - left_endpoint);

            for (int i = 1; i <= max; i++)
            {
                x2 = (x1 + xmid) / 2.0;
                cout << "The next midpoint, approximation is " <<fixed<<setprecision(7)<< x2 << endl;
                f2 = f(B, x2, p, degree);

                if (f1 * f2 <= 0.0) // root is in left half interval
                {
                    current_width = (x2 - x1) / 2.0;
                    fmid = f2;
                    xmid = x2;
                }

                else // root is in right half interval
                {
                    current_width = (xmid - x2) / 2.0;
                    f1 = f2;
                    x1 = x2;
                }

                if (current_width < epsilon)
                {
                    cout << "\nA root at x = " << x2 << " was found "
                    << "in " << i << " iterations" << endl;
                    f2 = f(B, x2, p, degree);
                    cout << "The value of the function at the root is " << f2 << endl;
                    break;
                }
            }
        }

        else if (j == 51)
        {
        cout << "\nThe search for a root has failed. The polynomial you have entered"
        << "\ncontains one or more complex roots." << endl;
        break;
        }

    }

return;
}// end of bisection

//---------------------------------------------------------------------------------------

double f(vector<double> B, double x, double p, int degree)// function to evaluate p(x)
{
    p = B[degree];
    for (int i = (degree - 1); i >= 0; i--)
    {
        p = p*x;
        p = p+B[i];
    }

return p;
}// end of f

//---------------------------------------------------------------------------------------

double fx(vector<double> B, double x, double px, int degree)// function to evaluate p'(x)
{
    for (int i = 1; i <= degree; i++)
    {
        B[i - 1] = i * B[i];
    }

    px = B[degree - 1];
    for (int i = (degree - 2); i >= 0; i--)
    {
        px = px*x;
        px = px+B[i];
    }

return px;
}// end of fx

//---------------------------------------------------------------------------------------

Hi,

please may you explain again what is happening because in the first post you said the error message is being printed out no matter what and in your second you mention you are dealing with complex roots and need the message but nothing happens at the moment.

Sorry to be confusing - before when I had j = 51, it was printing out the error message no matter what, and now that I have changed it to j == 51, no error message is being printed when it *should* i.e. when I give it a polynomial with complex roots.

Hi,

please may you explain again what is happening because in the first post you said the error message is being printed out no matter what and in your second you mention you are dealing with complex roots and need the message but nothing happens at the moment.

I think when you have complex roots none of your conditions are being met hence the error is not being printed. Your opening for loop:

#
for (double j = -50; j <= 51; j=j+0.1)

cout<<j<<endl; to see the result, I do not think it actually reaches 51 hence the if(j==51) does not turn true.

Have a look at it.

Oh thanks for that advice Grubschumi, I did that and realised j only goes up to 50.9.

Changed it to if(j==50.9) but still no error message?! Is there another way to do this other than with an if statement?

OK changed it to if(j>=50.9) and it now works...no idea why it wouldnt for == to though. Thanks for all the help.

Well so long as it works now .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.