eugene_bespoyasko

ChM_12

May 4th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. inline double function(double x, double y)
  7. {
  8.     return x + cos(y/sqrt(5));
  9. }
  10.  
  11. void Euler(const double x0, const double y0, const double x1, const double h)
  12. {
  13.     double x = x0, y = y0;
  14.     cout << "   Euler's method\nx = " << x << "\t y = " << y << endl;
  15.     while (x <= x1)
  16.     {
  17.         double f = function(x, y); //визначення приросту
  18.         y += h*f;
  19.         x += h;
  20.         cout << "x = " << x << "\t y = " << y << endl;
  21.     }
  22. }
  23.  
  24. void Runge_Kutta(const double x0, const double y0, const double x1, const double h)
  25. {
  26.     double x = x0, y = y0;
  27.     cout << "\nRunge-Kutta's method\nx = " << x << "\t y = " << y << endl;
  28.     while (x <= x1)
  29.     {
  30.         double k1 = h* function(x,y); //визначення коефіцієнтів
  31.         double k2 = h * function(x+h/2, y+k1/2);
  32.         double k3 = h * function(x+h/2, y+k2/2);
  33.         double k4 = h * function(x+h, y+k3);
  34.         double f = (k1+2*k2+2*k3+k4)/6; //визначення приросту
  35.         y += f;
  36.         x += h;
  37.         cout << "x = " << x << "\t y = " << y << endl;
  38.     }
  39. }
  40.  
  41. void Adams(const double x0, const double y0, const double x, const double h)
  42. {
  43.     double x4 = x0, y = y0, y4 = y0, x1 = x4+3*h, x2 = x4 + 2 * h, x3 = x4 + h;
  44.     double y3 = y4 + h * function(x4, y4);
  45.     double y2 = y3 + h*(3*function(x3, y3) - function(x4, y4))/2;
  46.     double y1 = y2 + h*(23*function(x2,y2)-16*function(x3, y3) + 5*(x4, y4))/12;
  47.     cout << "\n   Adams method\nx = " << x4 << "\t y = " << y4 << endl;
  48.     cout << "x = " << x3 << "\t y = " << y3 << endl;
  49.     cout << "x = " << x2 << "\t y = " << y2 << endl;
  50.     cout << "x = " << x1 << "\t y = " << y1 << endl;
  51.     while (x1 <= x)
  52.     {
  53.         y = y1 + h*(55*function(x1,y1)-59*function(x2,y2)+37*function(x3,y3)-9*function(x4,y4))/24;
  54.         y4 = y3;
  55.         y3 = y2;
  56.         y2 = y1;
  57.         y1 = y;
  58.         x4 = x3;
  59.         x3 = x2;
  60.         x2 = x1;
  61.         x1 += h;
  62.         cout << "x = " << x1 << "\t y = " << y << endl;
  63.     }
  64. }
  65.  
  66. int main()
  67. {
  68.     const double x0 = 1.8;
  69.     const double y0 = 2.6;
  70.     const double x1 = 2.8;
  71.     const int    n  = 10;
  72.     const double h  = (x1 - x0) / n;
  73.  
  74.     Euler(x0, y0, x1, h);
  75.     Runge_Kutta(x0, y0, x1, h);
  76.     Adams(x0, y0, x1, h);
  77.  
  78.     system("pause");
  79.     return 0;
  80. }
Add Comment
Please, Sign In to add comment