Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cmath>
- #define POSITIVE 1
- #define NEGATIVE -1
- using namespace std;
- class BisectionMethod{
- int limit;
- int lower_bound, upper_bound;
- double step;
- double eps;
- double next_i;
- public:
- BisectionMethod(int, int, double, double);
- double f(double);
- double* givePoints();
- double calcRoots(double, double);
- void displayRoots();
- int signOf(double);
- };
- inline double BisectionMethod::f(double x){
- return (x-1.11)*(x-1.12)*(x-1.13)*(x + 1.14);
- }
- inline int BisectionMethod::signOf(double val){
- if(val < 0){
- return NEGATIVE;
- }
- else{
- return POSITIVE;
- }
- }
- BisectionMethod::BisectionMethod(int lower_bound, int upper_bound, double step, double eps){
- BisectionMethod::upper_bound = upper_bound;
- BisectionMethod::lower_bound = lower_bound;
- BisectionMethod::step = step;
- BisectionMethod::next_i = lower_bound;
- BisectionMethod::eps = eps;
- limit = (int)((BisectionMethod::upper_bound - BisectionMethod::lower_bound) / BisectionMethod::step);
- }
- double* BisectionMethod::givePoints(){
- double curr_x, next_x;
- double *arr = new double[2];
- for(double i = lower_bound; i <= upper_bound; i+=0.01){
- //curr_x = lower_bound + (i * step);
- //next_x = lower_bound + ((i + 1) * step);
- curr_x = i;
- next_i = i + 1;
- // Check if f(curr_x) and f(next_x) are of opposite signs
- double f_curr_x = f(curr_x);
- double f_next_x = f(next_x);
- if((f_curr_x * f_next_x) < 0){
- next_i = i + 1;
- arr[0] = curr_x;
- arr[1] = next_x;
- cout << "f_curr_x = " << f_curr_x << endl;
- cout << "f_next_x = " << f_next_x << endl;
- cout << "Presence of root between " << arr[0] << " and " << arr[1] << endl;
- }
- }
- return arr;
- }
- double BisectionMethod::calcRoots(double a, double b){
- int count = 1;
- //cout << "SNO\t\ta(-ve)\t\tb(+ve)\t\tc\t\tf(c)\t\tSign of f(c)\n";
- double c;
- double fc;
- double last_fc;
- do{
- //cout << count << "\t\t" << a << "\t\t" << b << "\t\t";
- c = (a + b) / 2;
- fc = f(c);
- if(signOf(fc) == POSITIVE){
- b = c;
- }
- else {
- a = c;
- }
- //cout << c << "\t\t" << fc << "\t\t" << signOf(fc) << endl;
- //cin.get();
- if(count != 1){
- if((last_fc - fc) < eps){
- break;
- }
- }
- last_fc = fc;
- count++;
- }
- while(fabs(fc) > eps);
- return c;
- }
- void BisectionMethod::displayRoots(){
- double *arr = NULL;
- double root;
- do{
- arr = givePoints();
- root = calcRoots(arr[0], arr[1]);
- cout << "Root between " << arr[0] << " and " << arr[1] << " is " << root << endl;
- cin.get();
- delete arr;
- arr = NULL;
- }while(next_i != limit);
- }
- int main() {
- system("cls");
- /*
- double curr_x, next_x, lower_bound = -10;
- int limit = (int)((10-(-10)) / 0.01);
- for(int i = 0; i < limit; i++){
- curr_x = lower_bound + (i * 0.01);
- //next_x = lower_bound + ((i + 1) * step);
- cout << curr_x << endl;
- if(i % 20 == 0){
- cin.get();
- }
- }*/
- BisectionMethod obj(-10, 10, 1e-2, 1e-6);
- double *arr = obj.givePoints();
- //cout << "arr[0] = " << arr[0] << endl;
- //cout << "arr[1] = " << arr[1] << endl;
- //double root = obj.calcRoots(-1.73205, -1.73205);
- //cout << "Root = " << root << endl;
- //delete arr;
- obj.displayRoots();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment