Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bisection and False Position Method
- // Author : Shakib Haris
- // Date : 17 May 2017
- #include <iomanip>
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #define eps 0.01
- #define MAX 5 // maximum degree 4
- #define MAX_LOOP 100
- using namespace std;
- int degreeOfEquation;
- int numberOfRoots;
- int aN[MAX]; // array to hold the coefficients
- double ranges[MAX][2]; // array to hold the ranges for the roots
- double roots[MAX]; // array to hold the roots;
- bool confirm () {
- char ch;
- while (true) {
- cin.sync();
- ch = getchar();
- if (ch == 'y' || ch == 'Y' || ch == '\0')
- return true;
- else if (ch == 'n' || ch == 'N')
- return false;
- else
- cout << "Invalid input" << endl << "Try again!" << endl << "(Y/n): ";
- }
- }
- double f (double x) { //
- double total = 0.0;
- for (int i = 0; i <= degreeOfEquation; i++)
- total += aN[i] * pow (x, degreeOfEquation - i);
- return total;
- }
- void getRanges () {
- cout << "Find Ranges automatically? (Y/n): ";
- if (confirm()) {
- int xMax = abs ((int) ceil (sqrt (pow ((((double) aN[1]) / aN[0]), 2) - 2 * (((double) aN[2]) / aN[0]))));
- cout << "Xmax is " << xMax << endl;
- for (int i = (-1) * (xMax); i < xMax; i++) {
- if (f (i) == 0) {
- ranges[numberOfRoots][0] = i;
- ranges[numberOfRoots][1] = i;
- numberOfRoots++;
- }
- else if ((f (i) * f (i + 1)) < 0) {
- ranges[numberOfRoots][0] = i;
- ranges[numberOfRoots][1] = i + 1;
- numberOfRoots++;
- }
- }
- cout << "Found " << numberOfRoots << " Range(s)" << endl;
- }
- else {
- cout << "Number of ranges: ";
- cin >> numberOfRoots;
- for (int i = 0; i < numberOfRoots; i++) {
- cout << "Range " << i + 1 << " Lower Bound: ";
- cin >> ranges[i][0];
- cout << "Range " << i + 1 << " Upper Bound: ";
- cin >> ranges[i][1];
- }
- }
- // print ranges
- for (int i = 0; i < numberOfRoots; i++)
- cout << "Range " << i + 1 << ": " << ranges[i][0] << ", " << ranges[i][1] << endl;
- cout << endl << endl;
- }
- void getCoefficients () {
- cout << "Enter the Coefficients for all the degrees of X" << endl;
- for (int i = 0; i <= degreeOfEquation; i++) {
- cout << "An-" << i << ": ";
- cin >> aN[i];
- }
- }
- void bisect () {
- double x0, x1, x2;
- double f0, f1, f2;
- int loopCount;
- for (int i = 0; i < numberOfRoots; i++) {
- x1 = ranges[i][0];
- x2 = ranges[i][1];
- x0 = (x1 + x2) / 2;
- f1 = f(x1);
- f2 = f(x2);
- f0 = f(x0);
- loopCount = 0;
- while (fabs (f0) >= eps && loopCount < MAX_LOOP) {
- if (f0 * f1 < 0) {
- x2 = x0;
- f2 = f0;
- }
- else {
- x1 = x0;
- f1 = f0;
- }
- x0 = (x1 + x2) / 2;
- f0 = f(x0);
- loopCount++;
- }
- if (loopCount != MAX_LOOP) {
- roots[i] = x0;
- cout << "Found Root " << x0 << endl;
- }
- else {
- cout << "Root not Found for Range: " << ranges[i][0] << ", " << ranges[i][1] << endl;
- roots[i] = INT_MAX;
- }
- }
- }
- void falsePosition () {
- double x0, x1, x2;
- double f0, f1, f2;
- int loopCount;
- for (int i = 0; i < numberOfRoots; i++) {
- x1 = ranges[i][0];
- x2 = ranges[i][1];
- f1 = f(x1);
- f2 = f(x2);
- if (x1 != x2)
- x0 = x1 - (f1 * ((x2 - x1) / (f2 - f1)));
- else
- x0 = x1;
- f0 = f(x0);
- loopCount = 0;
- while (fabs (f0) >= eps && loopCount < MAX_LOOP) {
- if (f0 * f1 < 0) {
- x2 = x0;
- f2 = f0;
- }
- else {
- x1 = x0;
- f1 = f0;
- }
- x0 = x1 - (f1 * ((x2 - x1) / (f2 - f1)));
- f0 = f(x0);
- loopCount++;
- }
- if (loopCount != MAX_LOOP) {
- roots[i] = x0;
- cout << "Found Root " << x0 << endl;
- }
- else {
- cout << "Root not Found for Range: " << ranges[i][0] << ", " << ranges[i][1] << endl;
- roots[i] = INT_MAX;
- }
- }
- }
- int main () {
- cout << "Degree of the Equation: ";
- cin >> degreeOfEquation;
- // user inputs the coefficients
- getCoefficients ();
- system("cls");
- // getting ranges
- getRanges();
- if (numberOfRoots <= 0) {
- cout << "No Root Found" << endl;
- return 0;
- }
- // system("cls");
- cout << "running BISECTION Method" << endl;
- bisect ();
- cout << endl << endl;
- cout << "running FALSE-POSITION Method" << endl;
- falsePosition ();
- //system("cls");
- // printing the roots
- // cout << "Root(s): ";
- // for (int i = 0; i < numberOfRoots; i++)
- // cout << fixed << setprecision(4) << roots[i] << " ";
- // cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement