vadimk772336

Untitled

Feb 17th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cmath>
  4. #include <vector>
  5. #include <Windows.h>
  6. #include <stdlib.h>
  7. #include <fstream>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <iomanip>
  11. using namespace std;
  12.  
  13. /* Рандом даёт ток целое число, поэтому отказались от этого
  14.     int flag = 0;
  15.     double r,h;
  16.  
  17.     h = (Array[CountSegments + 1].x - Array[0].x) / CountSegments;
  18.     if (CountSegments % 2 == 0)
  19.         flag = 1;
  20.  
  21.     if (flag == 1) {
  22.         for (int i = 1; i < CountSegments + 1; i=i+2) {
  23.             r = (rand() % h + 1);
  24.             Array[i].x = h + r;
  25.             Array[i].y = (*f)(Array[i].x);
  26.             Array[i + 1].x = h - r;
  27.             Array[i + 1].y = (*f)(Array[i + 1].x);
  28.         }
  29.     }
  30.     else {
  31.         Array[1].x = Array[0].x+h;
  32.         Array[1].y = (*f)(Array[1].x);
  33.         for (int i = 2; i < CountSegments; i=i+2) {
  34.             r = (rand() % 3 + 1);
  35.             Array[i].x = h+r;
  36.             Array[i].y = (*f)(Array[i].x);
  37.             Array[i+1].x = h-r;
  38.             Array[i+1].y = (*f)(Array[i+1].x);
  39.         }
  40.  
  41.     }
  42.    
  43. */
  44.  
  45. typedef double(*functiontype)(double x);
  46. typedef struct Node
  47. {
  48.     double x, y;
  49. } Node;
  50. typedef double(*method)(double x, Node* Array, int Count, double* DD_massiv);
  51. typedef struct Interval
  52. {
  53.     double InitialNode, EndNode;
  54. } Interval;
  55. const double PI = 3.141592653589793238463;
  56.  
  57. //Равномерная сетка
  58. void ValueUniformTable(functiontype* f, Node* Array, double Initial, double End, int CountNodes)
  59. {
  60.     double step = abs(Initial - End) / (CountNodes - 1);
  61.     Array[0].x = Initial; Array[0].y = (*f)(Array[0].x);
  62.     for (int i = 1; i < CountNodes; i++)
  63.     {
  64.         Array[i].x = Array[i - 1].x + step;
  65.     }
  66. }
  67.  
  68. //Неравномерная сетка
  69. void ValueIrregularTable(functiontype* f, Node* Array, double Initial, double End, int CountSegments) //Инитиал и Енд запихать в Array
  70. {
  71.     int i;
  72.     double alpha,h;
  73.  
  74.     Array[0].x = Initial;
  75.     Array[CountSegments+1].x = End;
  76.     Array[0].y = (*f)(Array[0].x);
  77.     Array[CountSegments+1].y = (*f)(Array[CountSegments+1].x);
  78.  
  79.     h = (Array[CountSegments + 1].x - Array[0].x) / CountSegments;
  80.     alpha = 2 * PI / CountSegments;
  81.  
  82.     cout << Array[0].x << Array[0].y << endl;
  83.     for (i = 1; i < CountSegments; i++) {
  84.         Array[i].x = Array[i].x+h+cos(alpha*i);
  85.         Array[i].y = (*f)(Array[0].x);
  86.         cout << Array[i].x << Array[i].y << endl;
  87.     }
  88.     cout << Array[CountSegments + 1].x << Array[CountSegments + 1].y << endl;
  89.  
  90. }
  91.  
  92. double Myfunc(double x)
  93. {
  94.     return x * x;
  95. }
  96. functiontype Func = &Myfunc;
  97.  
  98. /* Принимает на вход коэффициенты матриц в виде массива. Заполняет массив прогоночных коэффициентов
  99.  matrix_coeffs - двумерный массив вида [[a1,b1,c1,d1],[a2,b2,c2,d2],...,[an,bn,cn,dn]
  100. надо сделать чтобы а1=с1=0, переделать под ашки
  101. */
  102. void tridiagonal_matrix_algorithm(double** matrix_coeffs, int matrix_size) {
  103.     int i;
  104.     double denominator;
  105.     double K, E, K_prev = 0, E_prev = 0; //Прогоночные коэффициенты
  106.     double* x_massiv = new double[matrix_size];
  107.  
  108.     double** coeffs_massiv; //Двумерный массив, хранящий прогоночные коэффициенты, 0 - Кси, 1 - Эта
  109.     coeffs_massiv = new double*[matrix_size+1];
  110.     for (i = 0; i < matrix_size; i++)
  111.         coeffs_massiv[i] = new double[2];
  112.  
  113.     coeffs_massiv[0][0] = 0; coeffs_massiv[0][1] = 0;
  114.     matrix_coeffs[0][0] = 0; matrix_coeffs[matrix_size - 1][2] = 0; //а1=с1=0
  115.    
  116.     //Прямой ход
  117.     for (i = 1; i <= matrix_size; i++) { //Ищем K_i+1 и E_i+1 на iом шаге
  118.         denominator = (matrix_coeffs[i - 1][0] * K_prev + matrix_coeffs[i - 1][1]);
  119.         K = -(matrix_coeffs[i - 1][2]) / denominator;
  120.         E = (matrix_coeffs[i - 1][3] - matrix_coeffs[i - 1][0] * E_prev) / denominator;
  121.         K_prev = K;
  122.         E_prev = E;
  123.         coeffs_massiv[i][0] = K; coeffs_massiv[i][1] = E;
  124.     }
  125.  
  126.     //Обратный ход
  127.     x_massiv[matrix_size - 1] = coeffs_massiv[matrix_size][1];
  128.     for (i = matrix_size - 2; i >= 0; i--) {
  129.         x_massiv[i] = coeffs_massiv[i + 1][0] * x_massiv[i + 1] + coeffs_massiv[i + 1][1];
  130.     }
  131.    
  132. }
  133.  
  134. void get_matrix_coeffs(double Initial, double End, int matrix_size, functiontype* f, double** matrix_coeffs, Node* Array) {
  135.     int i;
  136.     double h1,h2,h_prev;
  137.     //double* gamma_massiv = new double[matrix_size]; //Гаммы яв решением ур, т.е. ответом.
  138.  
  139.     matrix_coeffs = new double*[matrix_size];
  140.     for (i = 0; i < matrix_size; i++)
  141.         matrix_coeffs[i] = new double[3];
  142.  
  143.  
  144.     for (i = 1; i <= matrix_size; i++) {
  145.         h1 = Array[i].x - Array[i-1].x;  //i
  146.         h2 = Array[i+1].x - Array[i].x; //i+1
  147.         h_prev = h2;
  148.         matrix_coeffs[i - 1][0] = h1;
  149.         matrix_coeffs[i - 1][1] = 2*(h1+h2);
  150.         matrix_coeffs[i - 1][1] = h2;
  151.     }
  152.  
  153. }
  154.  
  155. int main()
  156. {
  157.     setlocale(LC_ALL, "RUS");
  158.     functiontype Func = &Myfunc;
  159.     double Initial = 0, End = 5;
  160.     int CountSegments;
  161.     cout << "Введите N: ";
  162.     cin >> CountSegments;
  163.  
  164.  
  165.     //Построение сетки
  166.     Node* ArrayUniformNodes = new Node[CountSegments+1];
  167.     //ValueUniformTable(&Func, ArrayUniformNodes, Initial, End, CountSegments);
  168.     ValueIrregularTable(&Func, ArrayUniformNodes, Initial, End, CountSegments);
  169.  
  170.     /*//Заполнение массива коэффициентами матрицы
  171.     double** matrix_coeffs;
  172.     get_matrix_coeffs(Initial,End, CountSegments, &Func, matrix_coeffs, ArrayUniformNodes);
  173.  
  174.     //Получение ответа в x_massiv
  175.     double* x_massiv = new double[CountSegments+1];
  176.     tridiagonal_matrix_algorithm(matrix_coeffs, CountSegments+1);
  177.     */
  178.  
  179.     cout << endl;
  180.     system("pause");
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment