andruhovski icon

SLAU

andruhovski | PRO | 11/06/16 02:21:35 PM UTC | 0 ⭐ | 858 👁️ | Never ⏰ | []
C++ |

2.66 KB

|

None

|

0 👍

/

0 👎

// Win32Lab.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    double a[4][5] = {  {  0.0309, -0.0179, -0.0353, 0.048,   0.082 },          
                        { -0.0179, 0.0114, 0.0203, -0.0298, -0.0481 },
                        { -0.0353, 0.0203, 0.0426, -0.0546, -0.1018 },
                        {  0.048, -0.0298, -0.0564, 0.0849,   0.1452 } };
 
    int size = 4;
    //cin >> size;
 
    vector <vector <long double> > matrix;
 
    // ??????? ????? ????? ?????? (size) x (size + 1),
    // c ?????? ??????? ????????? ??????    
    matrix.resize(size);
    for (int i = 0; i < size; i++)
    {
        matrix[i].resize(size + 1);
 
        for (int j = 0; j < size + 1; j++)
        {
            matrix[i][j] = a[i][j];
            //cin >> matrix[i][j];
        }
    }
 
    // ????????? ??????????? ???????? ???????
    long double eps;
    // cin >> eps;
    eps = 0.001;
    // ?????? ?????? ???????? ??????????? ?? ?????????? ????????,
    // ?????? ???????? ????? ????? ????? ? ???????, ?.?. size,
    // ?????? ???????? ?????? ?????????? ????????? ??? ??????
    vector <long double> previousVariableValues(size, 0.0);
 
    // ????? ????????? ???????????? ??????? ?? ??? ???, 
    // ???? ?? ????? ?????????? ??????????? ????????    
    while (true)
    {
        // ?????? ?????? ???????? ??????????? ?? ??????? ????       
        vector <long double> currentVariableValues(size);
 
        // ????????? ???????? ??????????? ?? ??????? ????????
        // ? ???????????? ? ?????????????? ?????????
        for (int i = 0; i < size; i++)
        {
            // ?????????????? i-?? ??????????? ????????? 
            // ?????????? ????? i-?? ?????? ???????
            currentVariableValues[i] = matrix[i][size];
 
            // ???????? ????? ?? ???? ???????? ?? i-?? ???????????
            for (int j = 0; j < size; j++)
            {
                if (i != j)
                {
                    currentVariableValues[i] -= matrix[i][j] * previousVariableValues[j];
                }
            }
 
            // ????? ?? ??????????? ??? i-?? ???????????
            currentVariableValues[i] /= matrix[i][i];
        }
 
        // ????????? ??????? ??????????? ???????????? ?????????? ????????
        long double error = 0.0;
 
        for (int i = 0; i < size; i++)
        {
            error += fabs(currentVariableValues[i] - previousVariableValues[i]);
        }
 
        // ???? ??????????? ???????? ??????????, ?? ????????? ???????
        if (error < eps)
        {
            break;
        }
 
        // ????????? ? ????????? ????????, ??? 
        // ??? ??????? ???????? ??????????? 
        // ?????????? ?????????? ?? ?????????? ????????
        previousVariableValues = currentVariableValues;
    }
 
    // ??????? ????????? ???????? ??????????? ? 8 ??????? ????????
    for (int i = 0; i < size; i++)
    {
        printf("%.8llf ", previousVariableValues[i]);
    }
 
    return 0;
}

Comments