Advertisement
999ms

Untitled

Mar 12th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.   auto dy1 = [&] (double y1, double y2) { return 500 * y2 - 501 * y1; };    
  6.   auto dy2 = [&] (double y1, double y2) { return 500 * y1 - 501 * y2; };
  7.  
  8.   double h = 0;
  9.   cout << "Write your precesion" << endl;
  10.   cin >> h;
  11.  
  12.  
  13.   double y1_0 = 5;
  14.   double y2_0 = -10;
  15.  
  16.   cout << "Write initial conditions" << endl;
  17.   cin >> y1_0;
  18.   cin >> y2_0;
  19.  
  20.   vector<double> y1s, y2s;
  21.   y1s.push_back(y1_0);
  22.   y2s.push_back(y2_0);
  23.  
  24.   vector<double> coefficients = { 55. / 24, -59. / 24, 37. / 24, -09. / 24 };
  25.   int cnt = 0;
  26.   // Eulers method
  27.   cout << "Getting start 4 points unsing Eulers method" << endl;
  28.   for (int i = 1; i < 4; i++) {
  29.     double nextY1 = y1s.back() + h * dy1(y1s.back(), y2s.back());
  30.     double nextY2 = y2s.back() + h * dy2(y1s.back(), y2s.back());
  31.     y1s.push_back(nextY1);
  32.     y2s.push_back(nextY2);
  33.     cnt++;
  34.     cout << fixed << setprecision(10) << "Iterations: " << cnt << " y1: " << y1s.back() << " y2: " << y2s.back() << endl;
  35.   }
  36.  
  37.   cout << "Write number of iterations" << endl;
  38.   int n;
  39.   cin >> n;
  40.   auto getNext = [&] (int i, const function<double(double, double)>& f, double y) {
  41.     double dlt = 0;
  42.     for (int j = 1; j <= 4; j++) {
  43.       dlt += coefficients[j - 1] * f(y1s[i - j], y2s[i - j]);
  44.     }
  45.     return y + h * dlt;
  46.   };
  47.   for (; cnt <= n; cnt++) {
  48.     double nextY1 = getNext(cnt, dy1, y1s.back());
  49.     double nextY2 = getNext(cnt, dy2, y2s.back());
  50.     y1s.push_back(nextY1);
  51.     y2s.push_back(nextY2);
  52.     cout << fixed << setprecision(10) << "Iterations: " << cnt << " y1: " << y1s.back() << " y2: " << y2s.back() << endl;
  53.   }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement