-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterativeMethod.py
More file actions
205 lines (159 loc) · 6.95 KB
/
Copy pathIterativeMethod.py
File metadata and controls
205 lines (159 loc) · 6.95 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#Eduardo Mestanza
import numpy as np
import math
import csv
#Iterative Method using Jacobi's Iterative Method
def jacobi_iter(xVector, e, maximumIteration):
num2 = float (2)
num3 = float (3)
num4 = float (4)
aMatrix = np.array([[1, 1/num2, 1/num3], [1/num2, 1, 1/num4], [1/num3, 1/num4, 1]])
bVector = np.array([0.1, 0.1, 0.1])
xExact = np.array([9/float (190), 28/float (475), 33/float (475)])
totalApprox = np.array([0.0, 0.0, 0.0])
randomVector = np.array([0.0, 0.0, 0.0])
jacNumIteration = 0
errorVector = np.zeros((1, aMatrix.shape[1]), dtype = "float64")
errorApprox = 0
numRandomVectors = 100
enoughIteration = False
with open("Jacobi Method results.csv", "w") as iterationFile:
iterationFileWriter = csv.writer(iterationFile)
iterationFileWriter.writerow(["The Random Vectors", "Approximations of said vectors", "Number of Iterations"])
iterationFile.close()
for x in range(numRandomVectors):
xVector = np.random.random((3,1))
initialApprox = 0
for i in range(3):
randomVector[i] = xVector[i, 0]
for i in range(3):
initialApprox += ((randomVector[i] - xExact[i]) ** 2)
initialApprox = math.sqrt(initialApprox)
xApprox = np.array([0.0, 0.0, 0.0])
for i in range(maximumIteration):
counter = i
changedXVector = np.zeros_like(xVector)
dMatrix = np.zeros_like(aMatrix)
for j in range(3):
dMatrix[j, j] = 1 / aMatrix[j, j]
upperSum = np.dot(aMatrix[j, j + 1:], xVector[j + 1:])
lowerSum = np.dot(aMatrix[j, :j], xVector[:j])
changedXVector[j] = dMatrix[j, j]*((bVector[j]) - (upperSum + lowerSum))
if np.allclose(xVector, changedXVector, e):
enoughIteration = True
break
xVector = changedXVector
for z in range(3):
xApprox[z] += changedXVector[z]
for i in range(3):
totalApprox[i] += xApprox[i]
if (not enoughIteration):
print("Need more Iterations!")
else:
with open("Jacobi Method results.csv", "a") as iterationFile:
iterationFileWriter = csv.writer(iterationFile)
iterationFileWriter.writerow([randomVector, initialApprox, counter])
iterationFile.close()
jacNumIteration += counter
for y in range(3):
totalApprox[y] = totalApprox[y] / float (numRandomVectors)
errorVector = xApprox - xExact
for i in range(errorVector.shape[0]):
errorApprox += ((errorVector[i]) ** 2)
errorApprox = math.sqrt(errorApprox)
jacNumIteration = jacNumIteration / float (numRandomVectors)
print "Jacobi iteration:"
print "Solution from the last randomly generated x Vector:"
print xVector
print(counter, " Iterations")
print ("Average Error Aprroximation: ", errorApprox)
print ("Average Number of Iterations: ", jacNumIteration)
return jacNumIteration
#Iterative Method using Gauss-Seidel Iterative Method
def gs_iter(xVector, e, maximumIteration):
num2 = float (2)
num3 = float (3)
num4 = float (4)
aMatrix = np.array([[1, 1/num2, 1/num3], [1/num2, 1, 1/num4], [1/num3, 1/num4, 1]])
bVector = np.array([0.1, 0.1, 0.1])
xExact = np.array([9/float (190), 28/float (475), 33/float (475)])
xApprox = np.array([0.0, 0.0, 0.0])
errorVector = np.zeros((1, aMatrix.shape[1]), dtype = "float64")
totalApprox = np.array([0.0, 0.0, 0.0])
randomVector = np.array([0.0, 0.0, 0.0])
gsNumIteration = 0
errorApprox = 0
lowerSumVector = np.zeros_like(xApprox)
upperSumVector = np.zeros_like(xApprox)
numRandomVectors = 100
enoughIteration = False
with open("Gauss-Seidel Method results.csv", "w") as iterationFile:
iterationFileWriter = csv.writer(iterationFile)
iterationFileWriter.writerow(["The Random Vectors", "Approximations of said vectors", "Number of Iterations"])
iterationFile.close()
for x in range(numRandomVectors):
initialApprox = 0
xVector = np.random.random((3,1))
for i in range(3):
randomVector[i] = xVector[i, 0]
for i in range(3):
initialApprox += ((randomVector[i] - xExact[i]) ** 2)
initialApprox = math.sqrt(initialApprox)
xApprox = np.array([0.0, 0.0, 0.0])
for i in range(maximumIteration):
counter = i
changedXVector = np.zeros_like(xVector)
dMatrix = np.zeros_like(aMatrix)
for k in range(3):
dMatrix[k, k] = 1 / aMatrix[k, k]
dMatrix[1, 0] = -1 * (aMatrix[1, 0] / (aMatrix[0, 0] * aMatrix[1, 1]))
dMatrix[2, 1] = -1 * (aMatrix[2, 1] / (aMatrix[1, 1] * aMatrix[2, 2]))
dMatrix[2, 0] = (-1 * aMatrix[1, 1] * aMatrix[2, 0]) + (aMatrix[1, 0] * aMatrix[2, 1])
dMatrix[2, 0] = (dMatrix[2, 0] / (aMatrix[0, 0] * aMatrix[1, 1] * aMatrix[2, 2]))
for j in range(3):
upperSumVector[j] = np.dot(aMatrix[j, j + 1:], xVector[j + 1:])
changedXVector[j] = np.dot(dMatrix[j, :], (bVector - upperSumVector))
if np.allclose(xVector, changedXVector, e):
enoughIteration = True
break
xVector = changedXVector
for z in range(3):
xApprox[z] += changedXVector[z]
for i in range(3):
totalApprox[i] += xApprox[i]
if (not enoughIteration):
print("Need more Iterations!")
else:
with open("Gauss-Seidel Method results.csv", "a") as iterationFile:
iterationFileWriter = csv.writer(iterationFile)
iterationFileWriter.writerow([randomVector, initialApprox, counter])
iterationFile.close()
gsNumIteration += counter
for y in range(3):
totalApprox[y] = totalApprox[y] / float (numRandomVectors)
errorVector = xApprox - xExact
for i in range(errorVector.shape[0]):
errorApprox += ((errorVector[i]) ** 2)
errorApprox = math.sqrt(errorApprox)
gsNumIteration = gsNumIteration / float (numRandomVectors)
print "Gauss-Seidel Iteration"
print "Solution from the last randomly generated x Vector:"
print xVector
print(counter, " Iterations")
print ("Average Error Aprroximation: ", errorApprox)
print ("Average Number of Iterations: ", gsNumIteration)
return gsNumIteration
def main():
num2 = float (2)
num3 = float (3)
num4 = float (4)
aMatrix = np.array([[1, 1/num2, 1/num3], [1/num2, 1, 1/num4], [1/num3, 1/num4, 1]])
xVector = np.zeros((3,1))
e = 0.00005
maximumIteration = 100
jacNumIteration = jacobi_iter(xVector, e, maximumIteration)
gsNumIteration = gs_iter(xVector, e, maximumIteration)
print "The ratio of the M jacobi iterations and the M Gauss-Seidel iterations is:"
print jacNumIteration / float(gsNumIteration)
if __name__ == '__main__':
main()