Beruflich Dokumente
Kultur Dokumente
6 Lineare Gleichungssysteme
Marc Wagner
SS 2014
6.3 Gauß-Elimination mit Rückwärtssubstitution
2.
3.
4.
5. // Gauss_backsubs.C
6.
7. // Loest das lineare Gleichungssystem A x = b mit Hilfe des Gauss-Algorithmus
8. // mit Rueckwaertssubstitution.
9.
10.
11.
12. // **********
13.
14.
15.
16. #include <math.h>
17. #include <stdio.h>
21.
22.
23. // **********
24.
25.
26.
28.
29. // #define __SKALIERTE_TEILPIVOTISIERUNG__
30.
31.
32.
33. // **********
34.
35.
36.
37. // Dimension von A, b und x.
46.
47. // Ergebnis-Vektor.
48. double x[N];
49.
50. // Permutation der Zeilen aufgrund von Pivotisierung.
51. int p[N];
52.
53.
54.
55. // **********
56.
57.
58.
59. // Erzeugt eine gleichmaessig verteilte Zufallszahl zwischen min und max.
60.
64. }
65.
66.
67.
68. // **********
69.
70.
71.
72. void Print()
73. {
74. int i1, i2;
75.
76. for(i1 = 0; i1 < N; i1++)
77. {
78. for(i2 = 0; i2 < N; i2++)
79. {
81. }
82.
83. fprintf(stdout, "| %+5.2lf\n", b[p[i1]]);
84. }
85.
87. }
88.
89.
90.
91. // **********
92.
93.
94.
95. int main(int argc, char **argv)
96. {
97. double d1, d2, d3;
99.
100. srand(0);
101. // srand((unsigned int)time(NULL));
102.
103.
104. // *****
105.
106.
107. // Generiere zufaellige Matrix A und zufaelligen Vektor b, Eintraege in
111. {
115. }
116.
118. }
119.
121.
125. Print();
126.
127.
128. // *****
129.
130.
131. // Sichere Matrix A und Vektor b fuer spaeteren Test.
132.
135.
139. {
140. A_org[i1][i2] = A[i1][i2];
141. }
142.
143. b_org[i1] = b[i1];
144. }
145.
146.
147. // *****
148.
149.
150. // Spaltenausraeumen.
151.
152. // Vorlesungsnotation:
153. // i1 ~ n-1
154. // i2 ~ j-1
155. // i3 ~ k-1
156.
157. #ifdef __SKALIERTE_TEILPIVOTISIERUNG__
158.
159. // Maximum fuer jede Zeile von A speichern, bevor A durch Gauss-Algorithmus
160. // ueberschrieben wird.
161.
165. {
169. {
170. if(fabs(A[i1][i2]) > A_ij_max[i1])
171. A_ij_max[i1] = fabs(A[i1][i2]);
172. }
173. }
174.
175. #endif
176.
179. {
183.
184. #ifdef __TEILPIVOTISIERUNG__
185.
186. for(i2 = i1+1; i2 < N; i2++)
187. {
192. #endif
193.
195.
199. {
206. #endif
207.
208. i2 = p[i1];
209. p[i1] = p[index];
216. {
217. d1 = A[p[i2]][i1] / A[p[i1]][i1];
218.
223. {
224. A[p[i2]][i3] -= d1 * A[p[i1]][i3];
225. }
226.
229.
230. Print();
231. }
232.
233.
234. // *****
235.
236.
237. // Rueckwaertssubstitution.
238.
239. for(i1 = N-1; i1 >= 0; i1--)
240. // Fuer alle Komponenten von x ...
241. {
243.
247. }
248.
250. }
251.
253.
257. }
258.
259. fprintf(stdout, "%+5.2lf ).\n\n", x[N-1]);
260.
261.
262. // *****
263.
264.
265. // Test.
266.
267. double b_check[N];
268.
269. for(i1 = 0; i1 < N; i1++)
270. {
271. b_check[i1] = 0.0;
272.
274. {
275. b_check[i1] += A_org[i1][i2] * x[i2];
276. }
277. }
278.
280.
284. }
285.
286. fprintf(stdout, "%+5.2lf ).\n\n", b_check[N-1]);
287.
288. fprintf(stdout, "b_check - b = ( ");
289.
290. // Abweichung fuer jede Komponente.
291.
292. for(i1 = 0; i1 < N-1; i1++)
293. {
294. fprintf(stdout, "%+.1e ", b_check[i1] - b_org[i1]);
295. }
296.
300.
301. double norm = 0.0;
302.
304. {
305. norm += pow(b_check[i1] - b_org[i1], 2.0);
306. }
307.
311.
312.
313. // *****
314.
315.
N = 4
-----
Keine Pivotisierung:
|b_check - b| = +1.74535e-15.
Teilpivotisierung:
+0.68 -0.21 +0.57 +0.60 | +0.82
-0.60 -0.33 +0.54 -0.44 | +0.11
-0.05 +0.26 -0.27 +0.03 | +0.90
+0.83 +0.27 +0.43 -0.72 | +0.21
|b_check - b| = +5.15537e-16.
Skalierte Teilpivotisierung:
|b_check - b| = +1.52081e-15.
N = 100
-------
Keine Pivotisierung:
|b_check - b| = +2.25693e-11.
Teilpivotisierung:
|b_check - b| = +1.46047e-12.
Skalierte Teilpivotisierung:
|b_check - b| = +3.28886e-13.