Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. const double a = 1;
  10. const double b = 3;
  11.  
  12. /*
  13. const double a = 1;
  14. const double b = 3;
  15. const double a1 = 1;
  16. const double a2 = 1;
  17. const double A = 2 * M_E;
  18. const double b1 = 1;
  19. const double b2 = 1;
  20. const double B = 2 * M_E * M_E * M_E;
  21. const double eps = 0.003;
  22.  
  23. string yx = "e^x";
  24. double y(double x) {
  25.     return exp(x);
  26. }
  27.  
  28. string qx = "sin(x)";
  29. double q(double x) {
  30.     return sin(x);
  31. }
  32.  
  33. string px = "cos(x)";
  34. double p(double x) {
  35.     return cos(x);
  36. }
  37.  
  38. string fx = "exp(x) + cos(x) * exp(x) + sin(x) * exp(x)";
  39. double f(double x) {
  40.     return exp(x) + cos(x) * exp(x) + sin(x) * exp(x);
  41. }
  42. */
  43.  
  44. const double a1 = 1;
  45. const double a2 = 1;
  46. const double A = 6;
  47. const double b1 = 1;
  48. const double b2 = 1;
  49. const double B = 30;
  50. const double eps = 0.1;
  51.  
  52. string yx = "2x^2";
  53. double y(double x) {
  54.     return 2 * x  * x;
  55. }
  56.  
  57. string qx = "sin(x)";
  58. double q(double x) {
  59.     return sin(x);
  60. }
  61.  
  62. string px = "cos(x)";
  63. double p(double x) {
  64.     return cos(x);
  65. }
  66.  
  67. string fx = "12x + 6x^2cos(x) + 2x^3sin(x)";
  68. double f(double x) {
  69.     return 4 + cos(x) * 4 * x + sin(x) * 2 * x * x;
  70. }
  71.  
  72. vector<double> TridiagMatrixAlg(const vector<vector<double> > & arrA, int n) {
  73.     double h = (b - a) / n;
  74.  
  75.     vector<double> fi;
  76.     for (int i = 0; i < n - 1; i++) {
  77.         fi.push_back(f(a + i * h));
  78.     }
  79.  
  80.     vector<double> m;
  81.     vector<double> k;
  82.     for (int i = 0; i < n - 1; i++) {
  83.         double xi = a + i * h;
  84.         m.push_back(-2 + h * p(xi));
  85.         k.push_back(1 - h * p(xi) + h * h * q(xi));
  86.     }
  87.  
  88.     vector<double> c;
  89.     c.push_back((a2 - a1 * h) / (m[0] * (a2 - a1 * h) + k[0] * a2));
  90.     vector<double> d;
  91.     d.push_back(k[0] * A * h / (a2 - a1 * h) + fi[0] * h * h);
  92.  
  93.     for (int i = 1; i < n - 1; i++) {
  94.         c.push_back(1 / (m[i] - k[i] * c[i - 1]));
  95.         d.push_back(fi[i] * h * h - k[i] * c[i - 1] * d[i - 1]);
  96.     }
  97.  
  98.     vector<double> y(n + 1);
  99.     y[n] = (b2 * c[n - 2] * d[n - 2] + B * h) /
  100.         (b2 * (1 + c[n - 2]) + b1 * h);
  101.     for (int i = n - 1; i > 0; i--) {
  102.         y[i] = c[i - 1] * (d[i - 1] - y[i + 1]);
  103.     }
  104.     y[0] = (a2 * y[1] - A * h) / (a2 - a1 * h);
  105.  
  106.     return y;
  107. }
  108.  
  109. vector<double> fDiffMethod(int n) {
  110.     double h = (b - a) / n;
  111.  
  112.     vector<vector<double> > arrA(n + 1);
  113.     vector<double> fi(n + 1);
  114.     for (int i = 0; i <= n; i++) {
  115.         arrA[i].resize(n + 1);
  116.         for (int j = 0; j <= n; j++)
  117.             arrA[i][j] = 0;
  118.     }
  119.     arrA[0][0] = a1 * h - a2;
  120.     arrA[0][1] = a2;
  121.     fi[0] = A * h;
  122.  
  123.     arrA[n][n - 1] = b2;
  124.     arrA[n][n] = b1 * h - b2;
  125.     fi[n] = B * h;
  126.  
  127.     for (int i = 1; i < n; i++) {
  128.         double xi = a + (i - 1) * h;
  129.         arrA[i][i - 1] = 1 - p(xi) * h + q(xi) * h * h;
  130.         arrA[i][i] = p(xi) * h - 2;
  131.         arrA[i][i + 1] = 1;
  132.     }
  133.  
  134.     for (int i = 1; i < n; i++)
  135.         fi[i] = f(a + (i - 1) * h) * h * h;
  136.  
  137.     return TridiagMatrixAlg(arrA, n);
  138. }
  139.  
  140. int main() {
  141.     cout << "y'' + " << px << "y' + " << qx << "y = " << fx << endl;
  142.  
  143.     int n = 4;
  144.     bool norm = true;
  145.     vector<double> y2 = fDiffMethod(n);
  146.     do {
  147.         vector<double> y1 = y2;
  148.         n *= 2;
  149.         y2 = fDiffMethod(n);
  150.         norm = true;
  151.         for (int i = 0; i < y1.size(); i++) {
  152.             if (fabs(y2[i * 2] - y1[i]) > eps)
  153.                 norm = false;
  154.         }
  155.     } while (!norm);
  156.  
  157.     cout << "y = " << yx << endl;
  158.     cout << "n = " << n << " eps = " << eps << endl;
  159.     for (int i = 0; i < y2.size(); i++) {
  160.         cout << "y = " << y(a + i * (b - a) / n) << " resY = " << y2[i] << endl;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement