Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- //#define endl '\n'
- #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
- vector<double> x = { -2, -1, 0, 1, 2, 3, 4 };
- vector<double> y = { -8.78, -5.36, -0.49, -7.81, 4.07, -6.77, -2.17 };
- vector<double> b(7);
- double f(double cur_x) {
- double ans = 0;
- for (int i = 0; i < y.size(); ++i) {
- double pr = y[i];
- for (int j = 0; j < x.size(); ++j) {
- if (i != j) {
- pr *= (cur_x - x[j]) / (x[i] - x[j]);
- }
- }
- ans += pr;
- }
- return ans;
- }
- void get_b() {
- b[0] = y[0];
- for (int i = 1; i < 7; ++i) {
- b[i] = y[i] - b[0];
- double k = x[i] - x[0];
- for (int j = 1; j <= i - 1; ++j) {
- b[i] -= b[j] * k;
- k *= x[i] - x[j];
- }
- b[i] /= k;
- }
- }
- double N(double cur_x) {
- double ans = b[0];
- double k = 1;
- for (int i = 0; i < 6; ++i) {
- k *= cur_x - x[i];
- ans += k * b[i + 1];
- }
- return ans;
- }
- signed main() {
- for (int i = -2; i <= 4; ++i) {
- cout << "f(" << i << ") = " << f(i) << endl;
- }
- cout << "f(" << -1.3 << ") = " << f(-1.3) << endl;
- cout << "f(" << 1.75 << ") = " << f(1.75) << endl;
- cout << endl << endl;
- get_b();
- for (int i = 0; i < 7; ++i) {
- cout << "B" << i + 1 << " = " << b[i] << endl;
- }
- cout << endl << endl;
- for (int i = -2; i <= 4; ++i) {
- cout << "f(" << i << ") = " << N(i) << endl;
- }
- cout << "f(" << -1.3 << ") = " << N(-1.3) << endl;
- cout << "f(" << 1.75 << ") = " << N(1.75) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment