Advertisement
Guest User

Untitled

a guest
May 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. const double np = 150;
  10.  
  11.  
  12.  
  13. struct wezly
  14. {
  15. public:
  16. wezly()
  17. {};
  18. vector <double> wezel;
  19. vector <double> wartosc;
  20. };
  21.  
  22. vector<double>punkty;
  23.  
  24.  
  25. void wprowadz(double &a, double &b, double &n)
  26. {
  27. cout << "Poczatek przedzialu: ";
  28. cin >> a;
  29. cout << "Koniec przedzialu: ";
  30. cin >> b;
  31. cout << "ilosc wezlow: ";
  32. cin >> n;
  33. }
  34.  
  35. wezly tablicuj(double a, double b, double n)
  36. {
  37. wezly x;
  38. double luka = abs((b - a) / n);
  39. for (double i = a; i <= b; i += luka)
  40. {
  41. x.wezel.push_back(i);
  42. }
  43. for (double i = 0; i <= n; i++)
  44. {
  45. x.wartosc.push_back(abs(cos(x.wezel[i])*x.wezel[i]));
  46. }
  47. return x;
  48. }
  49.  
  50. void wypunktuj(double a, double b)
  51. {
  52. double luka = abs((b - a) / np);
  53. for (double i = a; i <= b; i += luka)
  54. {
  55. punkty.push_back(i);
  56. }
  57. }
  58.  
  59. vector<double> wielomian(wezly x, double punkt, double n)
  60. {
  61. vector<double> wielomiany;
  62. double wielomian = 1.0;
  63. for (int i = 0; i <= n; i++)
  64. {
  65. wielomiany.push_back(wielomian);
  66. wielomian *= punkt - x.wezel[i];
  67. }
  68. return wielomiany;
  69. }
  70.  
  71. vector<vector<double>> IR_Newtona(wezly x, int n)
  72. {
  73. vector<vector<double>> choinka;
  74. for (int i = 0; i <= n; i++)
  75. {
  76. vector<double>wnetrze;
  77. for (int j = 0; j <= n; j++)
  78. wnetrze.push_back(0);
  79. choinka.push_back(wnetrze);
  80. }
  81. for (int i = 0; i <= n; i++)
  82. {
  83. choinka[0][i] = x.wartosc[i];
  84. }
  85. int counter = 1;
  86. for (int i = 1; i <= n; i++)
  87. {
  88. for (int j = 0; j <= n-i; j++)
  89. {
  90. choinka[i][j] = (choinka[i - 1][j + 1] - choinka[i - 1][j]) / (x.wezel[j + counter] - x.wezel[j]);
  91. }
  92. counter++;
  93. }
  94. return choinka;
  95. }
  96.  
  97. double Wzor_newtona(wezly x, double punkt, int n)
  98. {
  99. vector<vector<double>> choinka;
  100. vector<double>wielomiany;
  101. wielomiany = wielomian(x, punkt, n);
  102. choinka = IR_Newtona(x, n);
  103. double wartosc = 0.0;
  104. for (int i = 0; i <= n; i++)
  105. {
  106. wartosc += wielomiany[i] * choinka[i][0];
  107. }
  108. return wartosc;
  109. }
  110.  
  111. void wypisz(wezly x, double n)
  112. {
  113. for (int i = 0; i < np; i++)
  114. {
  115. cout << punkty[i] << endl;
  116. cout << abs(cos(punkty[i])*punkty[i]) << endl;
  117. cout << Wzor_newtona(x, punkty[i], n) << endl;
  118. }
  119. }
  120.  
  121. int main()
  122. {
  123. double a, b, n;
  124. wezly x;
  125. wprowadz(a, b, n);
  126. x = tablicuj(a, b, n);
  127. wypunktuj(a, b);
  128. wypisz(x, n);
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement