Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<cmath>
- using namespace std;
- double f(double x) {
- return exp(-x)-x;
- } //functia definita
- int main() {
- cout.precision(6);
- cout.setf(ios::fixed);
- double x, x00, x0, errmax, err, errrel, err0;
- int it, itmax;
- cout << "Insert the initial values x00, x0 = "; cin>>x00>>x0;
- cout << "The maximum error errmax="; cin >> errmax;
- cout << "Insert the max iter. no. itmax="; cin >> itmax;
- err = 1; it = 0;
- x = x0-f(x0)*(x0-x00) / (f(x0)-f(x00));
- cout << "\n------------------------DISPLAY RESULTS---------- -\n\n";
- cout << "\n"<<setw(4)<<"It"<<setw(12)<<"x"<<setw(12) << "err" <<setw(12) << "x00" << setw(12) << "x0"<< setw(12) << "err0" << setw(12) << "errrel"<< setw(12) << "f";
- cout << "\n-- ---------------------------------\n";
- while (err > errmax && it <= itmax) {
- it++;
- x = x0 - f(x0)*(x0 - x00) / (f(x0) - f(x00));
- err = fabs(x - x0);
- errrel = fabs((x - x0) / x0);
- err0 = fabs(f(x));
- cout << setw(4) << it << setw(12) << x << setw(12) << err << setw(12) << x00 << setw(12) << x0 << setw(12) << err0 << setw(12) << errrel << setw(12) << f(x) << endl;
- x00 = x0; //a egalat cu b pentru decalare interval
- x0 = x; //b egalat cu ultimul c calculat
- if (f(x) == 0) break;
- }
- if((err <= errmax && it <= itmax) || (f(x) == 0)) {
- cout << "\n\n"<<"The solution converges after "<<it<< " iterations:\n";
- cout << endl << "The solution is x=" << x << endl;
- }
- else {
- cout << "\n\n"<<"The sol.does not converges after " << it << " iterations.\n";
- cout << "The partial value at the imposed max. no. of iters. is x=" << x << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment