Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <cmath>
  4. #include <math.h>
  5. #include <vector>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9.  
  10. vector<double> funcY(int n, vector<double> x, vector<double> y)
  11. {
  12. for (int k = 0; k < n; k++){
  13. y[k] = sin(x[k]);
  14. }
  15. return y;
  16. }
  17. vector<double> funcX(int n, vector<double> x)
  18. {
  19. for (int k = 0; k < n; k++){
  20. x[k] = M_PI*k / 4 / n;
  21. }
  22. return x;
  23. }
  24. vector<double> Lagr(double point, vector<double> x)
  25. {
  26. vector<double> Lagra(x.size());
  27. for (int i = 0; i < x.size(); i++){
  28. Lagra[i] = 1;
  29. for (int j = 0; j <x.size(); j++){
  30. (j != i) ? Lagra[i] *= (point - x[j]) : 1;
  31. }
  32. }
  33. return Lagra;
  34. }
  35.  
  36. double Interp(double point, vector<double> x, vector<double> y)
  37. {
  38. double InterY = 0;
  39. for (int i = 0; i < x.size(); i++){
  40. InterY += Lagr(point, x)[i] / Lagr(x[i], x)[i] * y[i];
  41. }
  42. return InterY;
  43. }
  44. void main()
  45. {
  46. ofstream fout("Task 6.txt");
  47. int n = 4;
  48. vector<double> x(n), y(n), Lagra(n);
  49. x = funcX(n, x);
  50. y = funcY(n, x, y);
  51.  
  52. int N = 10;
  53. int add = 1;
  54. fout << "X" << endl;
  55. for (int k = 0; k < N+add; k++){
  56. if (k != (N +add - 1))
  57. {
  58. fout << M_PI*k / N / n << ", ";
  59. }
  60. else { fout << sin(M_PI*k / N / n); }
  61. }
  62.  
  63. fout << endl<< "FUNC" << endl;
  64. for (int k = 0; k < N+add; k++){
  65. if (k != (N+add - 1))
  66. {
  67. fout << sin(M_PI*k / N / n) << ", ";
  68. }
  69. else { fout << sin(M_PI*k / N / n); }
  70. }
  71.  
  72. fout << endl<<"INTERP" << endl;
  73. for (int k = 0; k < N+add; k++){
  74. if (k != (N+add - 1))
  75. {
  76. fout << Interp(M_PI*k / N / n, x, y) << ", ";
  77. }
  78. else { fout << Interp(M_PI*k / N / n, x, y); }
  79. }
  80. fout <<endl<< "DIFFERENCE" << endl;
  81. for (int k = 0; k < N+add; k++){
  82. if (k != (N +add- 1))
  83. {
  84. fout << Interp(M_PI*k / N / n, x, y) - sin(M_PI*k / N / n) << ", ";
  85. }
  86. else { fout << Interp(M_PI*k / N / n, x, y) - sin(M_PI*k / N / n); }
  87. }
  88. fout.close();
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement