Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #include <map>
- #include <iomanip>
- using namespace std;
- ifstream in("input.txt");
- ofstream out("output.txt");
- int FindN(double Eps) {
- int N = ceil(1 / sqrt(2 * Eps));
- return N;
- }
- double Y(double x, int N) {
- double a = 1, sum = 0;
- for (double k = 1; k <= N; ++k) {
- sum += cos(x * k) / (k * k);
- }
- return sum;
- }
- double FindPL(map<double, double> A, double x) {
- double sum = 0;
- for (auto it1 = A.begin(); it1 != A.end(); ++it1) {
- double mul = 1;
- for (auto it2 = A.begin(); it2 != A.end(); ++it2) {
- if (it1->first != it2->first)
- mul *= (x - it2->first) / (it1->first - it2->first);
- }
- sum += it1->second * mul;
- }
- return sum;
- }
- int main() {
- double Eps = 0.0001, a = 0, b = 1;
- int n = 8;
- double h = (b - a) / n;
- map<double, double> A;
- int N = FindN(Eps);
- for (double i = a; i <= b; i += h) {
- double y = Y(i, N);
- A[i] = y;
- }
- cout << "\nx\t\tf(x)\t\tPL(x)\t\tf(x) - PL(x)\n\n";
- cout << fixed;
- cout.precision(5);
- double max = 0;
- for (auto it = A.begin(); it != A.end(); ++it) {
- cout << it->first << "\t\t" << it->second << "\t\t" << FindPL(A, it->first) << "\t\t" << abs(it->second - FindPL(A, it->first)) << endl;
- if (max < abs(it->second - FindPL(A, it->first))) {
- max = abs(it->second - FindPL(A, it->first));
- }
- ++it;
- if (it == A.end()) {
- break;
- }
- --it;
- if (max < abs(Y(it->first + h / 2, N) - FindPL(A, it->first + h / 2))) {
- max = abs(Y(it->first + h / 2, N) - FindPL(A, it->first + h / 2));
- }
- 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;
- }
- cout << "\n\n" << N << "\t\t" << max;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement