Answer to Question #198118 in C++ for jay

Question #198118

The bisection method is an approximation method to find the roots of the given equation by repeatedly dividing the interval. This method will divide the interval until the resulting interval is found, which is extremely small. You are required to: i. write a pseudocode algorithm to determine the roots of a polynomial equation using Bisection Method. ii. develop a trace table to test the algorithm. iii.write C++ computer programs to determine the roots of an equation using Bisection Method.


1
Expert's answer
2021-05-25T04:35:31-0400


#include <bits/stdc++.h>
using namespace std;
float f(float x)
{
    return (x*x*x - 4*x - 9);
}
void bisect(float *x,float a,float b,int *itr)
{
    *x = (a + b)/2;
    ++(*itr);
    cout << "Iteration no." <<setw(3) << *itr<< "X = " << setw(7) << setprecision(5)<< *x << endl;
}
int main()
{
    int itr = 0, maxitr;
    float x, a, b, aerr, x1;
    cout << "Enter the value of a : ";
    cin >> a ;
    cout << "Enter the value of b : ";
    cin >> b ;
    cout << "Enter allowed error : ";
    cin >> aerr;
    cout<< "Enter the number of iterations : ";
    cin >> maxitr;
    cout << fixed;
    bisect(&x,a,b,&itr);
    do
    {
        if (f(a)*f(x) < 0)
            b = x;
        else
            a = x;
        bisect (&x1,a,b,&itr);
        if (fabs(x1-x) < aerr)
        {
            cout << "After" << itr << "iterations, root"<< "=" << setw(6) << setprecision(4)<< x1 << endl;
            return 0;
        }
        x = x1;
   } while (itr < maxitr);
   cout << "Solution does not converge, iterations not sufficient" << endl;
   return 1;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog