Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<math.h>
  4. #include<windows.h>
  5. #include<time.h>
  6.  
  7.  
  8. using namespace std;
  9. bool choice;
  10. double
  11. GetPerformanceCounter()
  12. {
  13. LARGE_INTEGER li;
  14. QueryPerformanceFrequency(&li);
  15. double
  16. freq =
  17. double
  18. (li.QuadPart) / 1000.0;
  19. QueryPerformanceCounter(&li);
  20. return
  21. double
  22. (li.QuadPart) / freq;
  23. }
  24. float funkcja(float x)
  25. {
  26. float y;
  27. if (!choice)
  28. y = 2*x*x + 5 * x + 2;
  29. else
  30. y = fabs(sin(x));
  31. return y;
  32. }
  33.  
  34. int main()
  35. {
  36. cout << "[0] Wielomian" << endl << "[1] Sinus " << endl;
  37. cin >> choice;
  38. double start, stop;
  39. double czas;
  40. int n;
  41. float y[300], x[300];
  42. float przedzial, a, b, x0, F = 0, h;
  43. cout << "Podaj liczbe wezlow" << endl;
  44. cin >> n;
  45. cout << "Podaj a" << endl;
  46. cin >> a;
  47. cout << "Podaj b" << endl;
  48. cin >> b;
  49. cout << endl;
  50.  
  51. przedzial = fabs(b - a) / (n - 1);
  52. x0 = a;
  53. for (int i = 0; i <= n - 1; i++)
  54. {
  55. y[i] = funkcja(x0);
  56. x[i] = x0;
  57. x0 += przedzial;
  58. }
  59. start = GetPerformanceCounter();
  60. for (int i = 1; i <= n - 1; i++){
  61. h = (x[i] - x[i - 1]);
  62. F += (y[i] + y[i - 1])*h / 2.0;
  63. }
  64. stop = GetPerformanceCounter();
  65. czas = stop - start;
  66. cout << "Czas wykonania obliczeń = " << endl;
  67. cout << czas << endl;
  68. cout << "Oto otrzymane wartosci : " << endl;
  69. cout << "Metoda Trapezow: " << F << endl;
  70. F = 0;
  71. start = 0;
  72. stop = 0;
  73. czas = 0;
  74. start = GetPerformanceCounter();
  75. for (int i = 1; i <= n - 1; i++)
  76. {
  77. h = (x[i] - x[i - 1]) / 2.0;
  78. F += (h / 3.0)*(y[i] + 4.0*funkcja((x[i] + x[i - 1]) / 2.0) + y[i - 1]);
  79. }
  80. stop = GetPerformanceCounter();
  81. czas = stop - start;
  82. cout << "Czas wykonania obliczeń = " << endl;
  83. cout << czas << endl;
  84. cout << "Metoda Simpsona:" << F << endl;
  85.  
  86. system("pause");
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement