//
you're reading...
Uncategorized

C++ The Bisection Method

bisection02b

 

#include<iostream>
#include<cmath>
using namespace std;

double  f (double  x) {
    return (pow(x,2) - 3);
}

int  main() {
    double a,b,c;
    int count = 0;
    cout.precision(17);
    cout <<"Enter a = ";  cin >> a;
    cout <<"Enter b = ";  cin >> b;
    do  {
        count++;
        c = (a + b)/2; 
        cout<< count << "\t" << c << "\t" << f(c) << endl;
        if( f(a) * f(c) < 0 ) {
            b = c;
        }
        else {
            a = c;
        }
    }
    while ( abs(a - b) > 1e-15 &&  abs(f(c)) > 1e-15 );
}
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: