Skip to content

Commit 2dfcb1c

Browse files
committedJul 27, 2024
chore: Refactor algorithm to run until error < delta
1 parent 7a748ef commit 2dfcb1c

File tree

3 files changed

+89
-23
lines changed

3 files changed

+89
-23
lines changed
 

Diff for: ‎OT Lab - 1 (26-07-24)/gauss-seidel.cpp

+31-11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int main() {
4646
}
4747

4848
vector<double> x(n, 0), y(n, 0);
49+
4950
// cout << "Enter initial values of x" << endl;
5051
for (int i = 0; i < n; i++) {
5152
// cout << "x:[" << i << "] = ";
@@ -58,25 +59,44 @@ int main() {
5859
// cout << endl;
5960

6061
// cout << endl << "Enter the allowed error: ";
61-
// int delta; cin >> delta;
62+
int delta;
63+
cin >> delta;
6264
// cout << endl;
6365

64-
int error = 1e9;
65-
while (iter--) {
66-
// do {
66+
// int count = 0;
67+
double maxError = 0;
68+
do {
6769
for (int i = 0; i < n; i++) {
68-
y[i] = (b[i] / a[i][i]);
70+
// y[i] = (b[i] / a[i][i]);
71+
y[i] = b[i];
6972
for (int j = 0; j < n; j++) {
70-
if (j == i)
71-
continue;
72-
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
73-
x[i] = y[i];
73+
// if (j == i)
74+
// continue;
75+
// y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
76+
// // x[i] = y[i];
77+
// }
78+
79+
if (i != j) {
80+
y[i] -= a[i][j] * x[j];
81+
}
7482
}
75-
// error = max(error, )
83+
y[i] /= a[i][i];
84+
}
85+
86+
maxError = 0;
87+
for (int i = 0; i < n; i++) {
88+
maxError = max(maxError, abs(x[i] - y[i]));
89+
x[i] = y[i];
90+
// cout << "x" << i + 1 << " = " << y[i] << " ";
7691
cout << "x" << i + 1 << " = " << fixed << setprecision(6) << y[i] << " ";
7792
}
7893
cout << endl;
79-
// } while(error > delta);
94+
// count++;
95+
} while (maxError > delta);
96+
97+
cout << "The final solution is:" << endl;
98+
for (int i = 0; i < n; i++) {
99+
cout << "x[" << i << "] = " << x[i] << endl;
80100
}
81101

82102
return 0;

Diff for: ‎input.txt

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
7
2-
10 2 -1 1 2 -1 1
3-
1 8 1 0 1 -1 0
4-
1 1 9 2 0 0 1
5-
0 -1 5 1 11 -1 1
6-
0 0 2 -1 10 0 0
7-
2 1 - 1 -2 10 1
8-
1 1 -1 -1 1 -1 9
9-
14 10 14 16 11 11 9
1+
3
2+
-7 3 1
3+
2 8 -3
4+
-5 -1 9
5+
4 10 -6
106
0
117
0
128
0
139
5
14-
0.001
10+
0.01

Diff for: ‎output.txt

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
The matrix is not diagonally dominant.
2-
The Gauss-Seidel method may not converge.
1+
x1 = -0.571429 x2 = 1.25 x3 = -0.666667
2+
x1 = -0.130952 x2 = 1.14286 x3 = -0.845238
3+
x1 = -0.202381 x2 = 0.965774 x3 = -0.612434
4+
x1 = -0.245016 x2 = 1.07093 x3 = -0.671792
5+
x1 = -0.208428 x2 = 1.05933 x3 = -0.683794
6+
x1 = -0.215114 x2 = 1.04568 x3 = -0.664756
7+
x1 = -0.218243 x2 = 1.05449 x3 = -0.669987
8+
x1 = -0.215215 x2 = 1.05332 x3 = -0.670747
9+
x1 = -0.215829 x2 = 1.05227 x3 = -0.669195
10+
x1 = -0.216054 x2 = 1.05301 x3 = -0.669652
11+
x1 = -0.215804 x2 = 1.05289 x3 = -0.669695
12+
x1 = -0.215859 x2 = 1.05282 x3 = -0.669569
13+
x1 = -0.215875 x2 = 1.05288 x3 = -0.669609
14+
x1 = -0.215854 x2 = 1.05287 x3 = -0.669611
15+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669601
16+
x1 = -0.21586 x2 = 1.05286 x3 = -0.669604
17+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
18+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669603
19+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
20+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
21+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
22+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
23+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
24+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
25+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
26+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
27+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
28+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
29+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
30+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
31+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
32+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
33+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
34+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
35+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
36+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
37+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
38+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
39+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
40+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
41+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
42+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
43+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
44+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
45+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
46+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
47+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
48+
x1 = -0.215859 x2 = 1.05286 x3 = -0.669604
49+
The final solution is:
50+
x[0] = -0.215859
51+
x[1] = 1.05286
52+
x[2] = -0.669604

0 commit comments

Comments
 (0)