Advertisement
andruhovski

SLAU

Nov 6th, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. // Win32Lab.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. using namespace std;
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11.     double a[4][5] = {  {  0.0309, -0.0179, -0.0353, 0.048,   0.082 },         
  12.                         { -0.0179, 0.0114, 0.0203, -0.0298, -0.0481 },
  13.                         { -0.0353, 0.0203, 0.0426, -0.0546, -0.1018 },
  14.                         {  0.048, -0.0298, -0.0564, 0.0849,   0.1452 } };
  15.  
  16.     int size = 4;
  17.     //cin >> size;
  18.  
  19.     vector <vector <long double> > matrix;
  20.  
  21.     // ??????? ????? ????? ?????? (size) x (size + 1),
  22.     // c ?????? ??????? ????????? ??????    
  23.     matrix.resize(size);
  24.     for (int i = 0; i < size; i++)
  25.     {
  26.         matrix[i].resize(size + 1);
  27.  
  28.         for (int j = 0; j < size + 1; j++)
  29.         {
  30.             matrix[i][j] = a[i][j];
  31.             //cin >> matrix[i][j];
  32.         }
  33.     }
  34.  
  35.     // ????????? ??????????? ???????? ???????
  36.     long double eps;
  37.     // cin >> eps;
  38.     eps = 0.001;
  39.     // ?????? ?????? ???????? ??????????? ?? ?????????? ????????,
  40.     // ?????? ???????? ????? ????? ????? ? ???????, ?.?. size,
  41.     // ?????? ???????? ?????? ?????????? ????????? ??? ??????
  42.     vector <long double> previousVariableValues(size, 0.0);
  43.  
  44.     // ????? ????????? ???????????? ??????? ?? ??? ???,
  45.     // ???? ?? ????? ?????????? ??????????? ????????    
  46.     while (true)
  47.     {
  48.         // ?????? ?????? ???????? ??????????? ?? ??????? ????      
  49.         vector <long double> currentVariableValues(size);
  50.  
  51.         // ????????? ???????? ??????????? ?? ??????? ????????
  52.         // ? ???????????? ? ?????????????? ?????????
  53.         for (int i = 0; i < size; i++)
  54.         {
  55.             // ?????????????? i-?? ??????????? ?????????
  56.             // ?????????? ????? i-?? ?????? ???????
  57.             currentVariableValues[i] = matrix[i][size];
  58.  
  59.             // ???????? ????? ?? ???? ???????? ?? i-?? ???????????
  60.             for (int j = 0; j < size; j++)
  61.             {
  62.                 if (i != j)
  63.                 {
  64.                     currentVariableValues[i] -= matrix[i][j] * previousVariableValues[j];
  65.                 }
  66.             }
  67.  
  68.             // ????? ?? ??????????? ??? i-?? ???????????
  69.             currentVariableValues[i] /= matrix[i][i];
  70.         }
  71.  
  72.         // ????????? ??????? ??????????? ???????????? ?????????? ????????
  73.         long double error = 0.0;
  74.  
  75.         for (int i = 0; i < size; i++)
  76.         {
  77.             error += fabs(currentVariableValues[i] - previousVariableValues[i]);
  78.         }
  79.  
  80.         // ???? ??????????? ???????? ??????????, ?? ????????? ???????
  81.         if (error < eps)
  82.         {
  83.             break;
  84.         }
  85.  
  86.         // ????????? ? ????????? ????????, ???
  87.         // ??? ??????? ???????? ???????????
  88.         // ?????????? ?????????? ?? ?????????? ????????
  89.         previousVariableValues = currentVariableValues;
  90.     }
  91.  
  92.     // ??????? ????????? ???????? ??????????? ? 8 ??????? ????????
  93.     for (int i = 0; i < size; i++)
  94.     {
  95.         printf("%.8llf ", previousVariableValues[i]);
  96.     }
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement