Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. /*#include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4. double fpochodna(double x, double y)
  5. {
  6.  
  7. }
  8. int main()
  9. {
  10.     system("PAUSE");
  11. }*/
  12. #include "pch.h"
  13. #include <iostream>
  14. using namespace std;
  15.  
  16. double f(double, double);
  17. double fi_rk2(double, double, double);
  18. double fi_rk4(double, double, double);
  19.  
  20. int main()
  21. {
  22.     double a, b, h;
  23.     const int N = 10;
  24.     cin >> a >> b;
  25.     h = (b - a) / static_cast<double>(N);
  26.    
  27.     double y = 0.1, x = a;
  28.     cout << "\nMetoda Eulera:\nx = " << x << " y = " << y << endl;
  29.     for (int i = 0; i < N; i++) {
  30.         y += h*f(x, y);
  31.         x += h;
  32.         cout << "x = " << x << " y = " << y << endl;
  33.     }
  34.     double(*fi)(double, double, double) = fi_rk2;
  35.     y = 1.0, x = a;
  36.     cout << "\nMetoda Rungego-Kutty (RK2):\nx = " << x << " y = " << y << endl;
  37.     for (int i = 0; i < N; ++i) {
  38.         y += h * fi(x, y, h);
  39.         x += h;
  40.         cout << "x = " << x << " y = " << y << endl;
  41.     }
  42.  
  43.     y = 1.0, x = a;
  44.     fi = fi_rk4;
  45.     cout << "\nMetoda Rungego-Kutty (RK4):\nx = " << x << " y = " << y << endl;
  46.     for (int i = 0; i < N; ++i) {
  47.         y += h * fi(x, y, h);
  48.         x += h;
  49.         cout << "x = " << x << " y = " << y << endl;
  50.     }
  51.     system("pause");
  52.     return 0;
  53. }
  54.  
  55. inline double f(double x, double y) {
  56.     return (y - x * x);
  57. }
  58.  
  59. double fi_rk2(double x, double y, double h) {
  60.     double k[2];
  61.     k[0] = f(x, y),
  62.         k[1] = f(x + h, y + h * k[0]);
  63.     return 0.5*(k[0] + k[1]);
  64. }
  65.  
  66. double fi_rk4(double x, double y, double h) {
  67.     double k[4];
  68.     k[0] = f(x, y),
  69.         k[1] = f(x + 0.5*h, y + 0.5*h*k[0]),
  70.         k[2] = f(x + 0.5*h, y + 0.5*h*k[1]),
  71.         k[3] = f(x + h, y + h * k[2]);
  72.     return (k[0] + 2.0*k[1] + 2.0*k[2] + k[3]) / 6.0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement