-
Notifications
You must be signed in to change notification settings - Fork 1
/
tutorial.py
69 lines (58 loc) · 1.19 KB
/
tutorial.py
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
# Tutorial: Gauss Verfahren
# Import the needed libraries
from numpy import *
from numpy.random import *
# Parameters:
n = 3
print '======================================'
# Initialization:
A = rand(n,n)*10
b = rand(n,1)*10
A_ = copy(A)
b_ = copy(b)
print range(0,n)
for j in range(0,n):
for i in range(0,n):
Vorfaktor = A[i][j]/A[j][j]
#print A[0]*Vorfaktor
if i > j:
A[i] = A[i] - A[j]*Vorfaktor
b[i] = b[i] - b[j]*Vorfaktor
#A[A<1e-12] = 0
#x3 = b3/A33
#x2 = (b2-x3*A23)/A22
#x1 = (b1-x2*A12-x3*A13)/A11
print A
# Init:
summe = 0
x=zeros((n,1))
N = n-1
# Get first x:
x[N] = b[N]/A[N,N]
# ===============================
for I in range(1,N+1):
i = N-I
summe = 0
for k in range(i+1,N+1):
summe = summe + A[i][k]*x[k]
x[i] = (b[i] - summe)/A[i,i]
# ===============================
b__ = zeros((n,1))
i = 0
k = 0
for zeile in A_:
i = 0
for eintrag in zeile:
b__[k] += eintrag*x[i]
i += 1
k += 1
i = 0
#print b__
#b__ = dot(A_,x)
print '========Berechnet:========'
print b__
#from pylab import *
#imshow(b__,interpolation = 'nearest')
#show()
print '========Erwartet:======='
print b_