Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int n; // liczba wezlow
  9. float xp;
  10. int option;
  11.  
  12. float sum_xi = 0;
  13. float sum_fi = 0;
  14. float sum_xi_kw = 0;
  15. float sum_xifi = 0;
  16.  
  17. cout << "Podaj liczbe wezlow: ";
  18. cin >> n;
  19.  
  20. float x[n];
  21. float y[n];
  22.  
  23. cout << "x0" << ": ";
  24. cin >> x[0];
  25. cout << "f0" << ": ";
  26. cin >> y[0];
  27.  
  28. for(int i = 1; i < n;) {
  29. float xi;
  30. cout << "x" << i << ": ";
  31. cin >> xi;
  32.  
  33. if(xi <= x[i - 1]) {
  34. cout << "Prosze podawac wezly w kolejnosci rosnacej" << endl;
  35. } else {
  36. x[i] = xi;
  37. cout << "f" << i << ": ";
  38. cin >> y[i];
  39. i++;
  40. }
  41. }
  42.  
  43.  
  44. cout << "Metoda najmniejszych kwadratow" << endl;
  45. cout << "1) cwiczenie 2" << endl;
  46. cout << "2) cwiczenie 4" << endl;
  47. cout << "Prosze wybrac opcje: ";
  48. cin >> option;
  49.  
  50. float w, w0, w1;
  51. float a0, a1;
  52.  
  53.  
  54. for(int i = 0; i < n; i++) {
  55. sum_xi += x[i];
  56.  
  57. if(option == 2) {
  58. y[i] = log(y[i]);
  59. }
  60.  
  61. sum_fi += y[i];
  62. sum_xi_kw += x[i] * x[i];
  63. sum_xifi += x[i] * y[i];
  64. }
  65.  
  66. w = (n * sum_xi_kw) - (sum_xi * sum_xi);
  67. w0 = (sum_fi * sum_xi_kw) - (sum_xifi * sum_xi);
  68. w1 = (n * sum_xifi) - (sum_xi * sum_fi);
  69.  
  70. a0 = w0 / w;
  71. a1 = w1 / w;
  72.  
  73. if(option == 1) {
  74. cout << "a0: " << a0 << endl;
  75. cout << "a1: " << a1 << endl;
  76. cout << "Wielomianix: " << a0 << " "
  77. << (a1 > 0 ? "+" : "") << a1 << "x" << endl;
  78. } else if(option == 2) {
  79. float expa0 = exp(a0);
  80. float exp1 = exp(a1);
  81.  
  82. cout << "Wielomianix: " << expa0
  83. << "*" << exp1 << "^x" << endl;
  84. }
  85.  
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement