Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <locale.h>
- #include <math.h>
- #include <malloc.h>
- #include <process.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <iostream>
- double lagrange(double*, double*, int, double);
- double line(double*, double*, int, double);
- void knot(int, int, int, double*, double*);
- void approx(int, int, int, int, double*, double*);
- using namespace std;
- void main()
- {
- setlocale(LC_CTYPE, "Russian");
- int a, b, m, n = 8;
- // double *arr_x = new double[10];
- //double *arr_y = new double[10];
- cout << "Введите начало интервала:" << endl;
- cin >> a;
- cout << "Введите конец интервала:" << endl;
- cin >> b;
- cout << "Введите количество точек интерполяции (не более 10!):" << endl;
- cin >> m;
- double *arr_x = new double[b - a];
- double *arr_y = new double[b - a];
- knot(m, a, b, arr_x, arr_y);
- approx(m, n, a, b, arr_x, arr_y);
- system("pause");
- }
- void knot(int m, int a, int b, double *arr_x, double *arr_y)
- {
- double x, y;
- cout << "Значение функции в узловых точках";
- printf("\n N \t X \t Y");
- for (int i = 1; i <= m; i++){
- x = a + (double)(i - 1)*(b - a) / (m - 1);
- y = pow(x, 3) - 5 * (pow(x, 2));
- arr_x[(i - 1)] = x;
- arr_y[(i - 1)] = y;
- printf("\n %d \t %.2lf \t %.6lf", i, x, y) ;
- }
- cout << endl;
- cout << "-------------------------------------" << endl;
- }
- void approx(int m, int n, int a, int b, double *arr_x, double *arr_y)
- {
- double x, y, yi, yl;
- cout <<"Значение функции в узловых точках и аппроксимация";
- printf("\n N\t| X | F(x) | Fi(x) |delta(Fi) | Fl(x) | delta(Fl)");
- for (int i = 1; i <= n; i++){
- x = a + (double)(i - 1)*(b - a) /(n - 1);
- y = pow(x, 3)-5*(pow(x,2));
- yi = lagrange(arr_x, arr_y, m, x);
- yl = line(arr_x, arr_y, m, x);
- printf("\n %d\t| %.2lf | %.6lf | %.6lf | %.6lf | %.6lf | %.6lf", i, x, y, yi, abs(y - yi), yl, abs(y - yl) );
- }
- puts("\n");
- }
- double lagrange(double *arr_x, double *arr_y, int m, double x)
- {
- double L = 0, z;
- for (int i = 0; i < m; i++){
- z = 1;
- for (int j = 0; j < m; j++){
- if (j != i)
- z *= (x - arr_x[j]) / (arr_x[i] - arr_x[j]);
- }
- L += z * arr_y[i];
- }
- return L;
- }
- double line(double *arr_x, double *arr_y, int m, double x)
- {
- double L;
- int el = 0;
- for (int i = 0; i<m; i++)
- if (x >= arr_x[i] && x <= arr_x[i])
- el = i;
- L = arr_y[el] + (arr_y[el + 1] - arr_y[el])*(x - arr_x[el]) / (arr_x[el + 1] - arr_x[el]);
- return L;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement