Advertisement
Infiniti_Inter

PL

Dec 23rd, 2020
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <map>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. ifstream in("input.txt");
  10. ofstream out("output.txt");
  11.  
  12. int FindN(double Eps) {
  13.     int N = ceil(1 / sqrt(2 * Eps));
  14.     return N;
  15. }
  16.  
  17.  
  18. double Y(double x, int N) {
  19.     double a = 1, sum = 0;
  20.     for (double k = 1; k <= N; ++k) {
  21.         sum += cos(x * k) / (k * k);
  22.     }
  23.     return sum;
  24. }
  25. double FindPL(map<double, double> A, double x) {
  26.     double sum = 0;
  27.     for (auto it1 = A.begin(); it1 != A.end(); ++it1) {
  28.         double mul = 1;
  29.         for (auto it2 = A.begin(); it2 != A.end(); ++it2) {
  30.             if (it1->first != it2->first)
  31.                 mul *= (x - it2->first) / (it1->first - it2->first);
  32.         }
  33.         sum += it1->second * mul;
  34.     }
  35.     return sum;
  36. }
  37.  
  38.  
  39. int main() {
  40.     double Eps = 0.0001, a = 0, b = 1;
  41.     int n = 8;
  42.     double h = (b - a) / n;
  43.     map<double, double> A;
  44.     int N = FindN(Eps);
  45.     for (double i = a; i <= b; i += h) {
  46.         double y = Y(i, N);
  47.         A[i] = y;
  48.     }
  49.     cout << "\nx\t\tf(x)\t\tPL(x)\t\tf(x) - PL(x)\n\n";
  50.     cout << fixed;
  51.     cout.precision(5);
  52.     double max = 0;
  53.     for (auto it = A.begin(); it != A.end(); ++it) {
  54.         cout << it->first << "\t\t" << it->second << "\t\t" << FindPL(A, it->first) << "\t\t" << abs(it->second - FindPL(A, it->first)) << endl;
  55.         if (max < abs(it->second - FindPL(A, it->first))) {
  56.             max = abs(it->second - FindPL(A, it->first));
  57.         }
  58.         ++it;
  59.         if (it == A.end()) {
  60.             break;
  61.         }
  62.         --it;
  63.         if (max < abs(Y(it->first + h / 2, N) - FindPL(A, it->first + h / 2))) {
  64.             max = abs(Y(it->first + h / 2, N) - FindPL(A, it->first + h / 2));
  65.         }
  66.         cout << it->first + h / 2 << "\t\t" << Y(it->first + h / 2, N) << "\t\t" << FindPL(A, it->first + h / 2) << "\t\t" << abs(Y(it->first + h / 2, N) - FindPL(A, it->first + h / 2)) << endl;
  67.     }
  68.     cout << "\n\n" << N << "\t\t" << max;
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement