Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <conio.h>
  4. #include <iostream>
  5. using namespace std;
  6. #define E 1e-6
  7.  
  8. void Eiler(double, double, double, double);
  9. void Runge_Kutta_4(double, double, double, double);
  10. void AdamsMeth(double a, double b, double y0, double h, double exp);
  11.  
  12. int main()
  13. {
  14. double a = 0.7, b = 1.7, h = 1e-1, y0 = 2.1;
  15. Eiler(a, b, h, y0);
  16. cout << endl;
  17. Runge_Kutta_4(a, b, h, y0);
  18. AdamsMeth(a, b, y0, h, E);
  19. _getch();
  20. return 0;
  21. }
  22.  
  23. double function(double X, double Y)
  24. {
  25. return X + cos(Y / 1.25);
  26. }
  27.  
  28. void Eiler(double A, double B, double h, double y)
  29. {
  30. double X[11], Y[11];
  31. Y[0] = y;
  32. for (int i = 0; i < 11; i++)
  33. {
  34. X[i] = A + i * h;
  35. }
  36. printf_s("Eiler method:\ni = %d, X = %lf, Y = %lf\n", 0, X[0], Y[0]);
  37. for (int i = 1; i < 11; i++)
  38. {
  39. Y[i] = Y[i - 1] + function(X[i - 1], Y[i - 1])*h;
  40. printf_s("i = %d, X = %lf, Y = %lf\n", i, X[i], Y[i]);
  41. }
  42. //printf_s("Answer: %lf\n", function(X[10], Y[10]));
  43. }
  44.  
  45. void Runge_Kutta_4(double A, double B, double h, double y)
  46. {
  47. double X[11], Y[11], dY[10], k1, k2, k3, k4;
  48. Y[0] = y;
  49. for (int i = 0; i < 11; i++)
  50. {
  51. X[i] = A + i * h;
  52. }
  53. printf_s("Runge-Kutta method:\ni = %d, X = %lf, Y = %lf\n", 0, X[0], Y[0]);
  54. for (int i = 1; i < 11; i++)
  55. {
  56. k1 = h * function(X[i - 1], Y[i - 1]);
  57. k2 = h * function(X[i - 1] + h / 2, Y[i - 1] + k1 / 2);
  58. k3 = h * function(X[i - 1] + h / 2, Y[i - 1] + k2 / 2);
  59. k4 = h * function(X[i - 1] + h, Y[i - 1] + k3);
  60. dY[i - 1] = k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6;
  61. Y[i] = Y[i - 1] + dY[i - 1];
  62. printf_s("i = %d, X = %lf, Y = %lf\n", i, X[i], Y[i]);
  63. }
  64. //printf_s("Answer: %lf\n", function(X[10], Y[10]));
  65. }
  66.  
  67. void AdamsMeth(double a, double b, double y0, double h, double exp) {
  68. double k[4], y[4], x[4];
  69. double X = a, Y = y0, dy, new_y;
  70. int count = 1;
  71. //Palkas();
  72. printf_s("\nAdams method:\n%d. x = %.2lf\ty = %.2lf\n", 0, X, Y);
  73. for (int i = 0; i < 4; i++) {
  74. y[i] = Y;
  75. x[i] = X;
  76. k[0] = h * function(X, Y);
  77. k[1] = h * function(X + h / 2, Y + k[0] / 2);
  78. k[2] = h * function(X + h / 2, Y + k[1] / 2);
  79. k[3] = h * function(X + h, Y + k[2]);
  80. dy = (k[0] + 2 * k[1] + 2 * k[2] + k[3]) / 6;
  81. Y += dy;
  82. X += h;
  83. printf("%d. x = %.2lf\ty = %.2lf\n", count, X, Y);
  84. count++;
  85. }
  86. while (X <= b) {
  87. Y = y[3] + h / 24 * (function(x[3], y[3]) * 55 - function(x[2], y[2]) * 59 + function(x[1], y[1]) * 37 - function(x[0], y[0]) * 9);
  88. X += h;
  89. while (1) {
  90. new_y = y[3] + h / 24 * (9 * function(X, Y) + 19 * function(x[3], y[3]) - 5 * function(x[2], y[2]) + function(x[1], y[1]));
  91. if (fabs(function(X, new_y) - function(X, Y)) < exp) break;
  92. Y = new_y;
  93. }
  94. Y = y[3] + h / 24 * (9 * function(X, new_y) + 19 * function(x[3], y[3]) - 5 * function(x[2], y[2]) + function(x[1], y[1]));
  95. printf("%d. x = %.2lf\ty = %.2lf\n", count, X, Y);
  96. count++;
  97. for (int i = 0; i < 3; i++) {
  98. y[i] = y[i + 1];
  99. x[i] = x[i + 1];
  100. }
  101. y[3] = Y;
  102. x[3] = X;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement