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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int p, maxi = 1000;
const int MAX = 1000;
double coeff[MAX], dcoeff[MAX];
double xplus, xminus;
double f(double);
double bisection(double, double);
double falsi(double, double);
double df(double);
double ddf(double);
int main()
{
int method, out, selection;
double guess1, guess2, value1, value2, etol;
double xnew[MAX], xplus[MAX], xminus[MAX];
int imax;
double x[MAX];
//Get Function
cout << "\tEnter the order of the equation: ";
cin >> p;
for(int i = p; i > 0; i--)
{
cout << "\tEnter the coefficient of x^" << i << ": ";
cin >> coeff[i];
}
cout << "\tEnter the constant term: ";
cin >> coeff[0];
cout << endl;
//Sign Change
do
{
cout << "\tEnter first initial guess: ";
cin >> guess1;
cout << "\tEnter second initial guess: ";
cin >> guess2;
value1 = f(guess1);
value2 = f(guess2);
if(value1 * value2 < 0)
{
cout << endl;
cout << "\tA root has been bracketed!";
cout << endl << endl;
}
else
{
cout << endl << endl;
cout << "\tNo root is bracketed! "
<< "Increase delta x or find another guess!";
cout << endl << endl;
}
}while(!((value1 * value2) < 0));
//Assigning xplus and xminus
if(value1 > 0)
{
xplus[0] = guess1;
xminus[0] = guess2;
}
else
{
xplus[0] = guess2;
xminus[0] = guess1;
}
//Final inputs
cout << "\tInitial guess which gives f(x) > 0 is : " << xplus[0];
cout << endl;
cout << "\tInitial guess which gives f(x) < 0 is : " << xminus[0];
cout << endl << endl;
cout << "\tSet maximum number of iterations: ";
cin >> imax;
//The Heart of Bisection Method
for(int i = 0; i < imax; i++)
{
xnew[i] = bisection(xplus[i], xminus[i]);
if(f(xnew[i])> 0)
{
xplus[i+1] = xnew[i];
xminus[i+1]=xminus[i];
}
else
{
xminus[i+1] = xnew[i];
xplus[i+1]=xplus[i];
}
}
//For Good Output.
cout << endl << endl;
cout << "\t___________________________________________________________________________________"<< endl;
cout << "\tIteration x+ x- f(x+) f(x-) midpoint error " << endl;
cout << "\t___________________________________________________________________________________" << endl;
for(int i = 0; i < imax; i++)
{
cout <<"\t" << setw(5) << i+1 << ' '
<< setw(10) << xplus[i] << ' '
<< setw(11) << xminus[i] << ' '
<< setw(12) << f(xplus[i]) << ' '
<< setw(12) << f(xminus[i]) << ' '
<< setw(10) << xnew[i] << ' '
<< setw(10) << xplus[i] - xminus[i] << ' '
<< endl;
}
cout << "\t_____________________________________________________________________________________"<< endl;
cout << endl << endl << endl;
cout << "\tThe root is approximately: ";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(20);
cout << "\t" << xnew[imax-1];
cout << "\n\tWith error of: " <<"\t\t\t" << xplus[imax] - xminus[imax];
cin.get();
cin.get();
return 0;
}
double f(double x)
{
int i;
double s = 0;
for(i = 0; i <= p; i++)
{
s += coeff[i] * pow(x, i);
}
return s;
}
double bisection(double xp, double xm)
{
return (xp + xm) * .5;
}
|