Bisection Method
void Bisection(double xl , double xu, double es, int maxi) {
double xr = 0, xrOld = 0, i = 0, ea = 0;
if(f(xl)* f(xu) > 0) {
cout << "No root" << endl;
return;
}
cout << "i\t" << "xl\t" << "f(xl)\t" << "xr\t" << "f(xr)\t" << "xu\t" << "f(xu)\t" << "ea\t" << endl;
do {
xrOld = xr;
xr = (xl + xu) / 2;
ea = abs((xr - xrOld) / xr) * 100;
if (i == 0)
cout << ++i << "\t" << xl << "\t" << f(xl) << "\t" << xr << "\t" << f(xr) << "\t" << xu << "\t" << f(xu)
<< "\t"
<< "--\t" << endl;
else
cout << ++i << "\t" << xl << "\t" << f(xl) << "\t" << xr << "\t" << f(xr) << "\t" << xu << "\t" << f(xu)
<< "\t"
<< ea << endl;
f(xl) * f(xr) < 0 ? xu = xr : xl = xr;
} while(ea > es && i < maxi);
}
False Position Method
void FalsePosition(double xl , double xu, double es, int maxi) {
double xr = 0, xrOld = 0, i = 0, ea = 0;
if(f(xl)* f(xu) > 0) {
cout << "No root" << endl;
return;
}
cout << "i\t" << "xl\t" << "f(xl)\t" << "xr\t" << "f(xr)\t" << "xu\t" << "f(xu)\t" << "ea\t" << endl;
do {
xrOld = xr;
xr = xu - (f(xu) * (xl - xu)) / (f(xl) - f(xu));
ea = abs((xr - xrOld) / xr) * 100;
if (i == 0)
cout << ++i << "\t" << xl << "\t" << f(xl) << "\t" << xr << "\t" << f(xr) << "\t" << xu << "\t" << f(xu)
<< "\t"
<< "--\t" << endl;
else
cout << ++i << "\t" << xl << "\t" << f(xl) << "\t" << xr << "\t" << f(xr) << "\t" << xu << "\t" << f(xu)
<< "\t"
<< ea << endl;
f(xl) * f(xr) < 0 ? xu = xr : xl = xr;
} while(ea > es && i < maxi);
}
Simple Fixed Point
void SimpleFixedPoint (double x0, double es, double maxi){
double xOld = 0, ea = 0;
int i = 0;
cout << "i\t" << "xi\t" << "f(x\t" << "ea\t" << endl;
do {
xOld = x0;
x0 = f(x0);
ea = abs((x0 - xOld) / x0) * 100;
if (i == 0)
cout << ++i << "\t" << x0 << "\t" << f(x0) << "\t" << "--\t" << endl;
else
cout << ++i << "\t" << x0 << "\t" << f(x0) << "\t" << ea << endl;
} while(ea > es && i < maxi);
}
Newton
void Newton(double x0, double es, double maxi) {
double xOld = 0, ea = 0;
int i = 0;
cout << "i\t" << "xi\t" << "f(xi)\t" << "f'(xi)" << "ea\t" << endl;
do {
xOld = x0;
x0 = df(x0);
ea = abs((x0 - xOld) / x0) * 100;
if (i == 0)
cout << ++i << "\t" << x0 << "\t" << f(x0) << "\t" << df(x0) << "\t" << "--" << endl;
else
cout << ++i << "\t" << x0 << "\t" << f(x0) << "\t" << df(x0) << "\t" << ea << endl;
} while(ea > es && i < maxi);
}0
Secant
void Secant(double x_1, double x0 , double es, int maxi){
double xr = 0, xrOld = 0, i = 0, ea = 0;
cout << "i\t" << "xi-1\t" << "f(xi-1)\t" << "xi\t" << "f(xi)\t" << "ea\t" << endl;
do {
xrOld = xr;
xr = x0 - (f(x0) * (x_1 - x0)) / (f(x_1) - f(x0));
ea = abs((xr - xrOld) / xr) * 100;
if (i == 0)
cout << ++i << "\t" << x_1 << "\t" << f(x_1) << "\t" << xr << "\t" << f(xr) << "\t" << "--\t" << endl;
else
cout << ++i << "\t" << x_1 << "\t" << f(x_1) << "\t" << xr << "\t" << f(xr) << "\t" << ea << endl;
x_1 = x0;
x0 = xr;
} while(ea > es && i < maxi);
}