Write a C++ program to solve the given equation using the bisection method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> using namespace std; #define EP 0.01 // Bisection_Method. The function is t4tutorials^3 - t4tutorials^2 + 2 double RESULT(double t4tutorials) { return t4tutorials*t4tutorials*t4tutorials - t4tutorials*t4tutorials + 2; } // Prints root of RESULT(t4tutorials) with error in EPSILON void Bisection_Method(double a, double b) { if (RESULT(a) * RESULT(b) >= 0) { cout << "You have not assumed right a and b\n"; return; } double c = a; while ((b-a) >= EP) { // Find middle point c = (a+b)/2; // Check if middle point is root if (RESULT(c) == 0.0) break; // Decide the side to repeat the steps else if (RESULT(c)*RESULT(a) < 0) b = c; else a = c; } cout << "The value of root is : " << c; } // main function int main() { double a =-44, b = 55; Bisection_Method(a, b); return 0; } |
Output
The value of root is : -0.995667