Advertisement
Georgiy031

Untitled

Nov 18th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. #define all(x) x.begin(), x.end()
  7. #define rall(x) x.rbegin(), x.rend()
  8. //#define endl '\n'
  9. #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  10. ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
  11.  
  12.  
  13. vector<double> x = { -2, -1, 0, 1, 2, 3, 4 };
  14. vector<double> y = { -8.78, -5.36, -0.49, -7.81, 4.07, -6.77, -2.17 };
  15.  
  16. vector<double> b(7);
  17.  
  18. double f(double cur_x) {
  19.     double ans = 0;
  20.     for (int i = 0; i < y.size(); ++i) {
  21.         double pr = y[i];
  22.         for (int j = 0; j < x.size(); ++j) {
  23.             if (i != j) {
  24.                 pr *= (cur_x - x[j]) / (x[i] - x[j]);
  25.             }
  26.         }
  27.         ans += pr;
  28.     }
  29.     return ans;
  30. }
  31.  
  32. void get_b() {
  33.     b[0] = y[0];
  34.     for (int i = 1; i < 7; ++i) {
  35.         b[i] = y[i] - b[0];
  36.         double k = x[i] - x[0];
  37.         for (int j = 1; j <= i - 1; ++j) {
  38.             b[i] -= b[j] * k;
  39.             k *= x[i] - x[j];
  40.         }
  41.         b[i] /= k;
  42.     }
  43. }
  44.  
  45. double N(double cur_x) {
  46.     double ans = b[0];
  47.     double k = 1;
  48.     for (int i = 0; i < 6; ++i) {
  49.         k *= cur_x - x[i];
  50.         ans += k * b[i + 1];
  51.     }
  52.     return ans;
  53. }
  54. signed main() {
  55.     for (int i = -2; i <= 4; ++i) {
  56.         cout << "f(" << i << ") = " << f(i) << endl;
  57.     }
  58.     cout << "f(" << -1.3 << ") = " << f(-1.3) << endl;
  59.     cout << "f(" << 1.75 << ") = " << f(1.75) << endl;
  60.  
  61.  
  62.     cout << endl << endl;
  63.     get_b();
  64.     for (int i = 0; i < 7; ++i) {
  65.         cout << "B" << i + 1 << " = " << b[i] << endl;
  66.     }
  67.  
  68.  
  69.     cout << endl << endl;
  70.     for (int i = -2; i <= 4; ++i) {
  71.         cout << "f(" << i << ") = " << N(i) << endl;
  72.     }
  73.     cout << "f(" << -1.3 << ") = " << N(-1.3) << endl;
  74.     cout << "f(" << 1.75 << ") = " << N(1.75) << endl;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement