Difference between revisions of "Regula-Falsi Method (False-Position Method)"

From CodeCodex

(Regula-Falsi Method (False-Position Method) for more information visit here http://j.mp/Ha988f, http://j.mp/Ha9bB7, http://j.mp/Ha9eN6, http://j.mp/Ha9fkl)
 
 
Line 66: Line 66:
  
 
</pre>
 
</pre>
 +
 +
[[Category:C++]]

Latest revision as of 19:33, 27 March 2012

Regula-Falsi Method (False-Position Method) for more information visit here http://j.mp/Ha988f, http://j.mp/Ha9bB7, http://j.mp/Ha9eN6, http://j.mp/Ha9fkl

/* 
 * File:   main.cpp
 * Author: Bangonkali
 *
 * Created on March 28, 2012, 12:37 AM
 * 
 * Regula-Falsi Method (False-Position Method)
 * http://www.physics.arizona.edu/~restrepo/475A/Notes/sourcea/node17.html
 * 
 */

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <limits.h> // max num of iterations -> ULONG_MAX

using namespace std;

double f(double x = 0) {
    // this is the function to find the root of
    // f(x) = (x+2)(x+2)(x+2); roots = -2;
    return (x*x*x) + (6*x*x) + (12*x) + 8;
    
    // return (x*x) - 4;
}

/*
 * 
 */
int main(int argc, char** argv) {
    
    double a = 2;
    double b = -5;
    
    double c = 0;
    double c0 = b;
    double output = 0;
    double e = 1e-12;
    
    unsigned int i = 0;
    while (i < 1e12) {
        c = a - ((f(a)*(b-a)) / (f(b)-f(a)));
        i++;
        
        if ((c - c0) < e * abs(c)) {
            output = c;
            break;
        } else {
            c0 = c;
            if (f(a)*f(c) > 0)
                a = c;
            else 
                b = c;
        }
    }
    
    cout << "iterations: " << i << endl;
    cout << "output: " << output << endl;
    
    return 0;
}