Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include "pch.h"
- #include <vector>
- #include <cmath>
- #include <iostream>
- using namespace std;
- const int n = 8; // степень многочлена
- const int a = -2, b = 1; // границы интервала
- double h = static_cast<double>(b - a) / n; // приращение узлов интерполяции
- #define expr (1+x+x*x)/(1+x*x+x*x*x*x) // исходная функция
- double* xCoords = new double[n + 1], // создание массивов узлов интерполяции и значений выражения в них
- *yCoords = new double[n + 1];
- void createCoords() { // заполнение массивов
- int i = 0;
- for (double x = a; x <= b; x += h) {
- *(xCoords + i) = x;
- *(yCoords + i) = expr;
- i++;
- }
- }
- double* massD = new double[n + 1]; // массив коэфицентов d
- void createMassD() { // и тут заполнение
- *(massD + 0) = *(yCoords + 0); // d0 = f(x0)
- for (int j = 1; j <= n; j++)
- for (int i = 0; i <= n - j; i++) {
- *(yCoords + i) = (*(yCoords + i + 1) - *(yCoords + i)) / (*(xCoords + i + j) - *(xCoords + i)); // магия копипасты
- *(massD + j) = *(yCoords + 0);
- }
- }
- void createMassD_() { // и тут заполнение
- massD[0] = yCoords[0]; // d0 = f(x0)
- vector<vector<double> > vec(n + 1, vector<double>(n + 1, 0));
- int t = 1, k = n - 1;
- for (int i = 0; i <= n; i++)
- vec[0][i] = yCoords[i];
- for (int i = 1; i <= n; i++) {
- for (int j = 0; j <= k; j++)
- vec[i][j] = (vec[i - 1][j] - vec[i - 1][j + 1]) / (xCoords[j] - xCoords[j + t]);
- t++;
- k--;
- }
- for (int i = 1; i <= n; i++)
- massD[i] = vec[i][0];
- }
- void createMassD__() { // и тут заполнение
- double y = 1.0, u = 0.0;
- int e = 1;
- massD[0] = yCoords[0]; // d0 = f(x0)
- for (int m = 1; m <= n; m++) {
- for (int i = 0; i <= m; i++) {
- for (int j = 0; j <= m, j != i; j++)
- y *= 1 / (xCoords[i] - xCoords[j]);
- y *= yCoords[i];
- u += y;
- y = 1.0;
- }
- massD[e] = u;
- u = 0.0;
- e++;
- }
- }
- double newton(double x_) { // тут считаем значения Pn(x)
- double value = *(massD + 0), p = 1;
- for (int i = 1; i < n; i++) {
- p = p * (x_ - *(xCoords + i - 1));
- value += *(massD + i) * p;
- }
- return value;
- }
- int main() {
- createCoords();
- createMassD();
- for (int i = 0; i <= n; i++)
- cout << massD[i] << " ";
- cout << "\n\n";
- createMassD_();
- for (int i = 0; i <= n; i++)
- cout << massD[i] << " ";
- cout << "\n\n";
- createMassD__();
- for (int i = 0; i <= n; i++)
- cout << massD[i] << " ";
- cout << "\n\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement