1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| #include "stdafx.h"
#include "stdio.h"
#include <iostream>
#include "math.h"
const double PI = 3.1415926536;
//метод поворотів якобі
bool isSimmetrial( double **coefficients, int numberOfEquation ) {
bool result = true;
int i, j;
for ( i = 0; i < numberOfEquation; i++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
if ( coefficients[i][j] != coefficients[j][i] ) {
result = false;
break;}}
if ( !result ) {break;}}
return result;}
int wrachenie( double **coefficients, int numberOfEquation,
double **solution, double precision ) {
int result = 1;
int i, j, k;
int maxI, maxJ;
double max, fi;
double** matricaPoworota;
matricaPoworota = new double*[numberOfEquation];
for ( i = 0; i < numberOfEquation; i ++ ) {
matricaPoworota[i] = new double[numberOfEquation];}
double** temp;
temp = new double*[numberOfEquation];
for ( i = 0; i < numberOfEquation; i ++ ) {
temp[i] = new double[numberOfEquation];}
double fault = 0.0;
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
fault = fault + coefficients[i][j]*coefficients[i][j];}}
fault = sqrt( 2*fault );
while ( fault > precision ) {
max = 0.0;
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
if ( coefficients[i][j] > 0 && coefficients[i][j] > max ) {
max = coefficients[i][j];
maxI = i;
maxJ = j;}
else if ( coefficients[i][j] < 0 && - coefficients[i][j] > max ) {
max = - coefficients[i][j];
maxI = i;
maxJ = j;}}}
for ( i = 0; i < numberOfEquation; i ++ ){
for ( j = 0; j < numberOfEquation; j ++ ){
matricaPoworota[i][j] = 0;}
matricaPoworota[i][i] = 1;}
if ( coefficients[maxI][maxI] == coefficients[maxJ][maxJ] ) {
matricaPoworota[maxI][maxI] = matricaPoworota[maxJ][maxJ] =
matricaPoworota[maxJ][maxI] = sqrt( 2.0 ) / 2.0;
matricaPoworota[maxI][maxJ] = - sqrt( 2.0 ) / 2.0;}
else {
fi = 0.5 * atan( ( 2.0 * coefficients[maxI][maxJ] ) /
( coefficients[maxI][maxI] - coefficients[maxJ][maxJ] ) );
matricaPoworota[maxI][maxI] = matricaPoworota[maxJ][maxJ] = cos( fi );
matricaPoworota[maxI][maxJ] = - sin( fi );
matricaPoworota[maxJ][maxI] = sin( fi );}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
temp[i][j] = 0.0;}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( k = 0; k < numberOfEquation; k ++ ) {
temp[i][j] = temp[i][j] + matricaPoworota[k][i] * coefficients[k][j];}}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
coefficients[i][j] = 0.0;}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( k = 0; k < numberOfEquation; k ++ ) {
coefficients[i][j] = coefficients[i][j] +
temp[i][k] * matricaPoworota[k][j];}}}
fault = 0.0;
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = i + 1; j < numberOfEquation; j ++ ) {
fault = fault + coefficients[i][j]*coefficients[i][j];}}
fault = sqrt( 2*fault );
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
temp[i][j] = 0.0;}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
for ( k = 0; k < numberOfEquation; k ++ ) {
temp[i][j] = temp[i][j] + solution[i][k] * matricaPoworota[k][j];}}}
for ( i = 0; i < numberOfEquation; i ++ ) {
for ( j = 0; j < numberOfEquation; j ++ ) {
solution[i][j] = temp[i][j];}}
result++;}
return result;}
using namespace std;
void main()
{
setlocale(LC_ALL,"rus");
int i, j;
int size;
double **coefficients, **solution, precision;
cout << "‚иконала - ‘Садова Ольга (КЉЌЉК - 11).";
cout << "\n\nМетод поворотiв Якобi.\nВведiть розмiрнiсть матрицi: ";
cin >> size;
cout << "\nВведiть елементи матрицi: \n";
coefficients = new double*[size];
solution = new double*[size];
for ( i = 0; i < size; i++ ) {
coefficients[i] = new double[size];
solution[i] = new double[size];}
for ( i = 0; i < size; i ++ ){
for ( j = 0; j < size; j ++ ){
solution[i][j] = 0;}
solution[i][i] = 1;}
for ( i = 0; i < size; i ++ ){
cout << "Enter " << i + 1 << " row: ";
for ( j = 0; j < size; j ++ ){
cin >> coefficients[i][j];}}
cout << "введiть точнiсть розрахунку: ";
cin >> precision;
if ( !isSimmetrial( coefficients, size ) ) {
cout << "матриця не симетрична";}
else {
int steps = wrachenie( coefficients, size, solution, precision );
cout << "Reshenie:\n";
for ( i = 0; i < size; i++ ) {
cout << "Особистий вектор k " << i + 1 << ":\n";
for ( j = 0; j < size; j ++ ){
cout << solution[j][i] << "\n";}}
cout << "Особистий значения:\n";
for ( i = 0; i < size; i++ ) {
cout << coefficients[i][i] << "\n";}
cout << "Загальне число крокiв: " << steps;}
cout << "\nPress "Enter" to continue..." << endl;
getchar();
system("pause");
} |