-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
82 lines (78 loc) · 2.96 KB
/
Copy pathMain.java
File metadata and controls
82 lines (78 loc) · 2.96 KB
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
public class Main {
public static void main(String[] args) {
final int size = 4;
double[] result = new double[size];
double[] previousValue = new double[size];
double accuracy = 0.0001;
int iteration = 0;
double[][] matrix = {
{9.89, 2.45, 3.35, 2.28, 10.98},
{1.5, 6.53, -0.3, -0.21, -1.83},
{2.25, 1.32, 5.08, 0.49, 6.97},
{30.07, -0.35, 1.05, 38.54, -3.56}
};
System.out.println("Матриця:");
printMatrix(matrix);
for (int i = 0; i < size; i++)
result[i] = 1;
while (!conditionOfEnd(result, previousValue, accuracy)) {
System.arraycopy(result, 0, previousValue, 0, size);
for (int i = 0; i < matrix.length; i++) {
double temp = 0;
for (int j = 0; j < result.length; j++) {
if (j != i)
temp = temp + (matrix[i][j] * result[j]);
}
result[i] = (matrix[i][matrix[i].length-1] - temp) / matrix[i][i];
}
iteration++;
if (iteration <= 3 || conditionOfEnd(result, previousValue, accuracy)) {
System.out.println("\n" +iteration + " Ітерація: " );
for (int i = 0; i < size; i++)
System.out.printf("x%d = %.5f\n", (i+1), result[i]);
double[] r = calculateNeviazka(matrix, result);
System.out.println("Вектор нев'язки:" );
printMatrix(r);
}
}
}
public static boolean conditionOfEnd(double[] result, double[] previousValue, double accuracy) {
double temp = 0;
for (int i = 0; i < result.length; i++)
temp += Math.abs(result[i] - previousValue[i]);
return temp < accuracy;
}
public static void printMatrix(double[][] matrix) {
for (double[] doubles : matrix) {
for (int j = 0; j < doubles.length; j++) {
if (j == 0)
System.out.printf("%7.4f", doubles[j]);
else
System.out.printf("%11.4f", doubles[j]);
}
System.out.println();
}
System.out.println();
}
public static void printMatrix(double[] matrix) {
for (double v : matrix) {
System.out.printf("%.6f ", v);
}
System.out.println();
}
public static double[] calculateNeviazka(double[][] matrix, double[] x) {
int n = x.length;
double[] r = new double[n];
for (int i = 0; i < n; i++) {
double sum = 0;
for (int j = 0; j < n; j++) {
sum = sum + matrix[i][j] * x[j];
}
r[i] = sum;
}
for (int i = 0; i < n; i++) {
r[i] = matrix[i][n] - r[i];
}
return r;
}
}