Advertisement
gashink_t

du_Eiler,Runge(lab_11, vm)

Apr 7th, 2021
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <memory>
  4. using namespace std;
  5.  
  6. float df(float x, float y) {
  7.     return cos(x) + 2 * y;
  8. }
  9.  
  10. void Eiler(float a, float b);
  11. void Runge(float a, float b);
  12.  
  13. int main() {
  14.     float a, b;
  15.     cout << "Enter a = "; cin >> a; cout << "Enter b = "; cin >> b;
  16.     Eiler(a, b);
  17.     Runge(a, b);
  18.     return 0;
  19. }
  20.  
  21. void Eiler(float a, float b)
  22. {
  23.     float h = 0.1;
  24.     float n = ((b - a) / h) + 1;
  25.     unique_ptr<float[]> X(new float[(int)n]);
  26.     unique_ptr<float[]> Y(new float[(int)n]);
  27.     X[0] = a;
  28.     cout << "Enter the Y[0] = "; cin >> Y[0];
  29.     for (int i = 1; i < n; i++) {
  30.         X[i] = a + i * h;
  31.         Y[i] = Y[i - 1] + h / 2 * (df(X[i - 1], Y[i - 1]) + df((X[i - 1] + h), (Y[i - 1] + h * df(X[i - 1], Y[i - 1]))));
  32.     }
  33.  
  34.     cout << "Table Eiler:\n";
  35.     for (int i = 0; i < n; i++) {
  36.         if (i == 0 || i == n - 1) cout << "X[" << i << "]=" << X[i] << "\t\tY[" << i << "]=" << Y[i] << endl;
  37.         else cout << "X[" << i << "]=" << X[i] << "\tY[" << i << "]=" << Y[i] << endl;
  38.     }
  39. }
  40.  
  41. void Runge(float a, float b)
  42. {
  43.     float h = 0.1;
  44.     float n = ((b - a) / h) + 1;
  45.     unique_ptr<float[]> X(new float[(int)n]);
  46.     unique_ptr<float[]> Y(new float[(int)n]);
  47.     X[0] = a;
  48.     cout << "\n\nEnter the Y[0] = "; cin >> Y[0];
  49.     float k1, k2;
  50.     for (int i = 1; i < n; i++)
  51.     {
  52.         k1 = h * df(X[i - 1], Y[i - 1]);
  53.         k2 = h * df(X[i - 1] + h, Y[i - 1] + k1);
  54.         X[i] = a + i * h;
  55.         Y[i] = Y[i - 1] + (k1 + k2)/2;
  56.     }
  57.     cout << "Table Runge:\n";
  58.     for (int i = 0; i < n; i++) {
  59.         if (i == 0 || i == n - 1) cout << "X[" << i << "]=" << X[i] << "\t\tY[" << i << "]=" << Y[i] << endl;
  60.         else cout << "X[" << i << "]=" << X[i] << "\tY[" << i << "]=" << Y[i] << endl;
  61.     }
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement