Answer to Question #189638 in C++ for jay

Question #189638

The bisection method is used to find the roots of a polynomial equation. It separates the interval and subdivides the interval in which the root of the equation lies. The principle behind this method is the intermediate theorem for continuous functions. It works by narrowing the gap between the positive and negative intervals until it closes in on the correct answer. This method narrows the gap by taking the average of the positive and negative intervals. For any continuous function f(x), i. Find TWO (2) points, say a and b such that a < b and f(a)* f(b) < 0 ii. Find the midpoint of a and b, say “t” iii. t is the root of the given function if f(t) = 0; else follow the next step iv. Divide the interval [a, b] v. If f(t)*f(b) <0, let a = t vi. Else if f(t) *f(a), let b = t vii. Repeat above three steps until f(t) = 0.


1
Expert's answer
2021-05-05T16:34:38-0400
#include <iostream>
using namespace std;
float f(float x){
    return x * x * x - x * x + 2;
}
int main(){
    //using an example of f(x) = x^3 - x^2 + 2
    float a = -500, b = 100, t;
    if(a < b && f(a) * f(b) < 0){
    while(f(a) * f(b) < 0){
        t = (a + b)/2;
        if(f(t) == 0.0) break;
        else if(f(t) * f(b) < 0) a = t;
        else if(f(t) * f(a) < 0) b = t;
        }
        cout<<"Root of x^3 - x^2 + 2: "<<t;
    }
    else cout<<"f(a) * f(b) > 0";
    return 0;
}

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
APPROVED BY CLIENTS