Advertisement
MaPV

VP_5_sopr_gradient

Jan 8th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int L = 9;
  9. int N = 3;
  10.  
  11. double sk (vector<double>&a,vector<double> &b){ //скалярное прозведение
  12. double c=0;
  13. for (int i = 0; i < N ;i++)
  14.     c+=a[i]*b[i];
  15. return c;
  16. }
  17.  
  18. void mv (vector <vector<double>>&a,vector<double>&b,vector<double>&c)
  19. {//умножение матрицы на столбец
  20. for (int i=0;i<N;i++)
  21.     for(int j=0;j<N;j++)
  22.     c[i]+=a[i][j]*b[j];
  23. }
  24.  
  25. void crt (vector<vector<double>> &a,vector<double>&b){
  26.     cout << "Введите матрицу А: " << endl;
  27.     for (int i = 0; i < N; i++){
  28.         for (int j = 0; j < N; j++){
  29.             cin >> a[i][j];
  30.         }
  31.     }
  32.     cout << "Введите стоблец свободных членов:" << endl;
  33.     for (int i = 0; i < N; i++){
  34.         cin >> b[i];
  35.     }
  36.     cout << endl;
  37. }
  38. void prnt (vector<vector<double>> &a){
  39.    
  40.     for (int i = 0; i < N; i++){
  41.         for (int j = 0; j < N; j++){
  42.             cout << a[i][j] << " ";
  43.         }
  44.         cout << "\n";
  45.     }
  46.     cout << "\n";
  47. }
  48. void prntB (vector<double>& a){
  49.     for (int i = 0; i < N; i++){
  50.         cout <<a[i] << " ";
  51.     }
  52.     cout << "\n";
  53. }
  54. bool converge (vector<double> &a, double e){
  55. double norm;
  56. norm=sk(a,a);
  57. if (sqrt(norm)>=e)
  58.     return false;
  59. return true;
  60. }
  61.  
  62. void GRADIENTIREN(vector<vector<double>>&a,vector<double>&b){
  63. int k=0;
  64. double e;
  65. cout << "Введите точность вычислений : ";
  66. cin >> e;
  67. cout << endl;
  68. vector<vector<double>>x(100,vector<double>(N)),r(100,vector<double>(N)),z(100,vector<double>(N));
  69. vector<double>res(N);
  70. double ak,bk;
  71. //x[0]={1,2,3}; //начальное приближение
  72. mv(a,x[0],res);
  73. for(int i=0;i<N;i++){ //podgotovka
  74.     r[0][i]=b[i]-res[i]; //невязка
  75.     z[0][i]=r[0][i]; //базисный вектор
  76.     }
  77. res={0,0,0};
  78. do{
  79. k++;
  80. mv(a,z[k-1],res);
  81. ak=(sk(r[k-1],r[k-1]))/(sk(res,z[k-1]));
  82. for (int i = 0;i < N;i++){
  83.     x[k][i]=x[k-1][i]+(ak*z[k-1][i]);
  84.     r[k][i]=r[k-1][i]-(ak*(res[i]));
  85. }
  86. res={0,0,0};
  87. bk=sk(r[k],r[k])/sk(r[k-1],r[k-1]);
  88. for (int i=0;i<N;i++)
  89.     z[k][i]=r[k][i]+bk*z[k-1][i];
  90. cout<<k<<"-я итерация : "<<endl;
  91. for (int i = 0; i < N; i++){
  92.     cout << "X" << i + 1 << " = " << x[k][i] << fixed << setprecision (L) << endl;
  93. }
  94. }//while (k<N);
  95. while (sqrt(sk(r[k],r[k]))>=e);
  96.  
  97.  
  98. //вывод решения
  99. cout << "\nРешение системы:" << endl;
  100. for (int i = 0; i < N; i++){
  101.     cout << "X" << i + 1 << " = " << x[k][i] << fixed << setprecision (L) << endl;
  102. }
  103. cout << "Число итераций: " << k  << endl;
  104.  
  105. }
  106.  
  107. int main (){
  108.     setlocale(LC_ALL,"ru");
  109.     vector<vector<double>>A(N,vector<double>(N));
  110.     vector<double>B(N);
  111. /*  A[0]={9,4,3};
  112.     A[1]={4,-3,0};
  113.     A[2]={3,0,5};
  114.     B[0]=1;
  115.     B[1]=5;
  116.     B[2]=9;*/
  117.     crt(A,B);
  118.     prnt(A);
  119.     prntB(B);
  120.     GRADIENTIREN(A,B);
  121.     system("pause");
  122.     return 0;  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement