Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <locale.h>
  4. #include <math.h>
  5. #include <malloc.h>
  6. #include <process.h>
  7. #include <stdlib.h>
  8. #include <windows.h>
  9. #include <iostream>
  10. double lagrange(double*, double*, int, double);
  11. double line(double*, double*, int, double);
  12. void knot(int, int, int, double*, double*);
  13. void approx(int, int, int, int, double*, double*);
  14. using namespace std;
  15. void main()
  16. {
  17. setlocale(LC_CTYPE, "Russian");
  18.  
  19. int a, b, m, n = 8;
  20. // double *arr_x = new double[10];
  21. //double *arr_y = new double[10];
  22.  
  23. cout << "Введите начало интервала:" << endl;
  24. cin >> a;
  25.  
  26. cout << "Введите конец интервала:" << endl;
  27. cin >> b;
  28.  
  29. cout << "Введите количество точек интерполяции (не более 10!):" << endl;
  30. cin >> m;
  31. double *arr_x = new double[b - a];
  32. double *arr_y = new double[b - a];
  33.  
  34. knot(m, a, b, arr_x, arr_y);
  35. approx(m, n, a, b, arr_x, arr_y);
  36. system("pause");
  37.  
  38. }
  39.  
  40. void knot(int m, int a, int b, double *arr_x, double *arr_y)
  41. {
  42. double x, y;
  43.  
  44. cout << "Значение функции в узловых точках";
  45. printf("\n N \t X \t Y");
  46.  
  47. for (int i = 1; i <= m; i++){
  48. x = a + (double)(i - 1)*(b - a) / (m - 1);
  49. y = pow(x, 3) - 5 * (pow(x, 2));
  50.  
  51. arr_x[(i - 1)] = x;
  52. arr_y[(i - 1)] = y;
  53. printf("\n %d \t %.2lf \t %.6lf", i, x, y) ;
  54. }
  55. cout << endl;
  56.  
  57. cout << "-------------------------------------" << endl;
  58.  
  59. }
  60.  
  61. void approx(int m, int n, int a, int b, double *arr_x, double *arr_y)
  62. {
  63. double x, y, yi, yl;
  64.  
  65. cout <<"Значение функции в узловых точках и аппроксимация";
  66. printf("\n N\t| X | F(x) | Fi(x) |delta(Fi) | Fl(x) | delta(Fl)");
  67. for (int i = 1; i <= n; i++){
  68. x = a + (double)(i - 1)*(b - a) /(n - 1);
  69. y = pow(x, 3)-5*(pow(x,2));
  70. yi = lagrange(arr_x, arr_y, m, x);
  71. yl = line(arr_x, arr_y, m, x);
  72. printf("\n %d\t| %.2lf | %.6lf | %.6lf | %.6lf | %.6lf | %.6lf", i, x, y, yi, abs(y - yi), yl, abs(y - yl) );
  73. }
  74. puts("\n");
  75. }
  76.  
  77. double lagrange(double *arr_x, double *arr_y, int m, double x)
  78. {
  79. double L = 0, z;
  80.  
  81. for (int i = 0; i < m; i++){
  82. z = 1;
  83. for (int j = 0; j < m; j++){
  84. if (j != i)
  85. z *= (x - arr_x[j]) / (arr_x[i] - arr_x[j]);
  86. }
  87. L += z * arr_y[i];
  88.  
  89. }
  90.  
  91. return L;
  92. }
  93.  
  94. double line(double *arr_x, double *arr_y, int m, double x)
  95. {
  96. double L;
  97. int el = 0;
  98.  
  99. for (int i = 0; i<m; i++)
  100. if (x >= arr_x[i] && x <= arr_x[i])
  101. el = i;
  102.  
  103. L = arr_y[el] + (arr_y[el + 1] - arr_y[el])*(x - arr_x[el]) / (arr_x[el + 1] - arr_x[el]);
  104.  
  105. return L;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement