Alx09

Untitled

May 25th, 2021
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. double f(double x) {
  7.     return exp(-x)-x;
  8. } //functia definita
  9.  
  10. int main() {
  11.     cout.precision(6);
  12.     cout.setf(ios::fixed);
  13.     double x, x00, x0, errmax, err, errrel, err0;
  14.     int it, itmax;
  15.     cout << "Insert the initial values x00, x0 = "; cin>>x00>>x0;
  16.     cout << "The maximum error errmax="; cin >> errmax;
  17.     cout << "Insert the max iter. no. itmax="; cin >> itmax;
  18.     err = 1; it = 0;
  19.     x = x0-f(x0)*(x0-x00) / (f(x0)-f(x00));
  20.     cout << "\n------------------------DISPLAY RESULTS---------- -\n\n";
  21.     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";
  22.     cout << "\n-- ---------------------------------\n";
  23.     while (err > errmax && it <= itmax) {
  24.         it++;
  25.         x = x0 - f(x0)*(x0 - x00) / (f(x0) - f(x00));
  26.         err = fabs(x - x0);
  27.         errrel = fabs((x - x0) / x0);
  28.         err0 = fabs(f(x));
  29.         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;
  30.         x00 = x0; //a egalat cu b pentru decalare interval
  31.         x0 = x; //b egalat cu ultimul c calculat
  32.         if (f(x) == 0) break;
  33.     }
  34.     if((err <= errmax && it <= itmax) || (f(x) == 0)) {
  35.         cout << "\n\n"<<"The solution converges after "<<it<< " iterations:\n";
  36.         cout << endl << "The solution is x=" << x << endl;
  37.     }
  38.  else {
  39.  
  40.  cout << "\n\n"<<"The sol.does not converges after " << it << " iterations.\n";
  41.  cout << "The partial value at the imposed max. no. of iters. is x=" << x << "\n";
  42.     }
  43.  
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment