Implement a task finding the root of a function f(x). You are to implement the Bisection method.

Question

Implement a task finding the root of a function f(x). You are to implement the Bisection method.
The program will have the following inputs: desired accuracy of the approximation (tolerance) and endpoints of the interval containing a solution. For the input, you are to prompt the user with questions (use cout) and take in answers (use cin).
So the output will be the desired approximate solution for f(x) = 0.
Such that the function f(x) can implement hard-coded as a type double function in the main program.
To test your code, you should use the function:
f(x) = x^3+x-5
You are to enter the tolerance of 0.0001,
and the interval endpoints Left = 0 and Right =1 and
Left =0 and Right = 2.
For the first case, your code should have a clever way of determining that there is no solution (hint, look at f(0) and f(1) ).
For the second case, your code should give an answer of
x = 1.51599 is an approximate solution of f(x) = 0 to within 0.0001

Summary

Here in this question we have a function x3 + x-5 And have to find its root by using a Bisection method. With the given intervals. And the tolerance is 0.0001. Such that we find a root with intervals. In the Bisection method, it will always check that the middle value is the root or not between the two intervals. So if it is not the root then the root between two intervals is found from some calculations.

Explanation

implement the Bisection method.

So in the figure, we can see left and right intervals as a1 and b1. After finding the middle value by the formula  (a1+b1)/2. And then check whether the answer is the root or not of the function. If the answer is yes it is the root then the root has to be returned. And if not then we have to find the range in the middle of starting and middle or middle and ending which is going to be considered. Now after the calculation is done if the value is negative then that range will get considered for the next iteration in the same process. So now the tolerance value is found with the difference and the root of a function is also get found. After such a process, we will have our output on the screen.

Code

#include <iostream>
using namespace std;

/* the function to find the root */
double function(double x){
    return x*x*x + x - 5;
}

/* to print the root */
void Bisection(double left,double right,double epsilon)
{
    double mid=left;
    while ((right-left) >= epsilon){
        /* middle point between two intervals */
        mid=(left+right)/2;

        /* to check if middle point is the root or not  */
        if (function(mid) == 0.0)
            break;

        /* to decide on the side for the root */
        else if (function(mid)*function(left) < 0)
            right=mid;
        else
            left=mid;
    }
    cout<<"The root for the function is: "<<mid;
}

/* main code */
int main()
{
    double epsilon;
    cout<<"Enter the tolerance value: ";
    cin>>epsilon;
    double left,right;
    cout<<"Enter the left interval: ";
    cin>>left;
    cout<<"Enter the right interval: ";
    cin>>right;
    Bisection(left,right,epsilon);
    return 0;
}

Output

 

Also read, draw the memory map of the following code and write the output of the program.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *