-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
199 lines (170 loc) · 6.84 KB
/
main.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
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
import csv
import pandas
from bokeh.plotting import figure
from bokeh.io import output_file, show, save
from bokeh.layouts import column
import glob
'''
Created by Sebastian Pajak
Solve set of equations
-2x1 + x2 = 0
x1 + -2x2 +x3 = 0
x2 +-2x3 +x4 =
1x3 - 2x4 = 1
'''
def define_matrix():
matrix = [[-2, 1, 0, 0, 0],
[1, -2, 1, 0, 0],
[0, 1, -2, 1, 0],
[0, 0, 1, -2, -1]]
return matrix
def jacobi_method(matrix):
i = 0
x1_old = 0
x2_old = 0
x3_old = 0
x4_old = 0
x1_new = x1_old
x2_new = x2_old
x3_new = x3_old
x4_new = x4_old
res = 1
residua_to_plot = {}
while res > 0.0000001:
residua_to_plot.update({i: res})
x1_new = (matrix[0][4] - matrix[0][1]*x2_old - matrix[0][2]*x3_old - matrix[0][3]*x4_old) / matrix[0][0]
x2_new = (matrix[1][4] - matrix[1][0]*x1_old - matrix[1][2]*x3_old - matrix[1][3]*x4_old) / matrix[1][1]
x3_new = (matrix[2][4] - matrix[2][0]*x1_old - matrix[2][1]*x2_old - matrix[2][3]*x4_old) / matrix[2][2]
x4_new = (matrix[3][4] - matrix[3][0]*x1_old - matrix[3][1]*x2_old - matrix[3][2]*x3_old) / matrix[3][3]
res = abs(x1_new + x2_new + x3_new + x4_new - x1_old - x2_old - x3_old - x4_old)
x1_old = x1_new
x2_old = x2_new
x3_old = x3_new
x4_old = x4_new
i += 1
with open('data.csv', 'a+', newline='') as csvfile:
field_names = ['Method', 'Iterations']
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writeheader()
writer.writerow({'Method': 'Jacobi', 'Iterations': str(i)})
with open('jacobi_residua.csv','w', newline='') as csvfile:
field_names = ['Iteration', 'Residuum']
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writeheader()
for k,v in residua_to_plot.items():
writer.writerow({'Iteration': k, 'Residuum':v})
return(x1_new, x2_new, x3_new, x4_new)
def gauss_siedl(matrix):
i = 0
x1_old = 0
x2_old = 0
x3_old = 0
x4_old = 0
x1_new = x1_old
x2_new = x2_old
x3_new = x3_old
x4_new = x4_old
res = 1
residua_to_plot = {}
while res > 0.0000001:
residua_to_plot.update({i: res})
x1_new = (matrix[0][4] - matrix[0][1]*x2_old - matrix[0][2]*x3_old - matrix[0][3]*x4_old) / matrix[0][0]
x2_new = (matrix[1][4] - matrix[1][0]*x1_new - matrix[1][2]*x3_old - matrix[1][3]*x4_old) / matrix[1][1]
x3_new = (matrix[2][4] - matrix[2][0]*x1_new - matrix[2][1]*x2_new - matrix[2][3]*x4_old) / matrix[2][2]
x4_new = (matrix[3][4] - matrix[3][0]*x1_new - matrix[3][1]*x2_new - matrix[3][2]*x3_new) / matrix[3][3]
res = abs(x1_new + x2_new + x3_new + x4_new - x1_old - x2_old - x3_old - x4_old)
x1_old = x1_new
x2_old = x2_new
x3_old = x3_new
x4_old = x4_new
i += 1
with open('data.csv', 'a+', newline='') as csvfile:
field_names = ['Method', 'Iterations']
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writerow({'Method': 'Gauss-Siedl', 'Iterations': str(i)})
with open('gauss_siedl_residua.csv','w', newline='') as csvfile:
field_names = ['Iteration', 'Residuum']
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writeheader()
for k,v in residua_to_plot.items():
writer.writerow({'Iteration': k, 'Residuum':v})
return(x1_new, x2_new, x3_new, x4_new)
def sor(matrix):
i = 0
x1_old = 0
x2_old = 0
x3_old = 0
x4_old = 0
x1_new = x1_old
x2_new = x2_old
x3_new = x3_old
x4_new = x4_old
res = 1
omega = 1.2
residua_to_plot = {}
while res > 0.0000001:
residua_to_plot.update({i: res})
x1_new = omega*(matrix[0][4] - matrix[0][1]*x2_old - matrix[0][2]*x3_old - matrix[0][3]*x4_old) / matrix[0][0] + (1-omega)*x1_old
x2_new = omega*(matrix[1][4] - matrix[1][0]*x1_new - matrix[1][2]*x3_old - matrix[1][3]*x4_old) / matrix[1][1] + (1-omega)*x2_old
x3_new = omega*(matrix[2][4] - matrix[2][0]*x1_new - matrix[2][1]*x2_new - matrix[2][3]*x4_old) / matrix[2][2] + (1-omega)*x3_old
x4_new = omega*(matrix[3][4] - matrix[3][0]*x1_new - matrix[3][1]*x2_new - matrix[3][2]*x3_new) / matrix[3][3] + (1-omega)*x4_old
res = abs(x1_new + x2_new + x3_new + x4_new - x1_old - x2_old - x3_old - x4_old)
x1_old = x1_new
x2_old = x2_new
x3_old = x3_new
x4_old = x4_new
i += 1
with open('data.csv', 'a+', newline='') as csvfile:
field_names = ['Method', 'Iterations']
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writerow({'Method': 'SOR 1.2', 'Iterations': str(i)})
with open('sor_residua.csv','w', newline='') as csvfile:
field_names = ['Iteration', 'Residuum']
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writeheader()
for k,v in residua_to_plot.items():
writer.writerow({'Iteration': k, 'Residuum':v})
return(x1_new, x2_new, x3_new, x4_new)
def plotting_data():
#add to list all files with extensions .csv
csv_data = []
for file in glob.glob("*.csv"):
csv_data.append(file)
#import data from files
df_res_jacobi = pandas.read_csv(csv_data[2])
df_res_gauss_siedl = pandas.read_csv(csv_data[1])
df_res_sor = pandas.read_csv(csv_data[3])
df_compare_methods = pandas.read_csv(csv_data[0])
#data for plotting
x1 = df_res_jacobi["Iteration"]
y1 = df_res_jacobi["Residuum"]
x2 = df_res_gauss_siedl["Iteration"]
y2 = df_res_gauss_siedl["Residuum"]
x3 = df_res_sor["Iteration"]
y3 = df_res_sor["Residuum"]
x4 = df_compare_methods["Method"]
y4 = df_compare_methods["Iterations"]
# output to static HTML file
output_file("iteration_methods.html", title="line plot example")
p4 = figure(x_range=x4, plot_height=350, title="Iterations per method")
p1 = figure(width=500, height=300, title="Jacobi")
p2 = figure(width=500, height=300, title="Gauss-Siedl")
p3 = figure(width=500, height=300, title="SOR 1.2")
p1.line(x1, y1)
p1.xaxis.axis_label = "Iterations"
p1.yaxis.axis_label = "Residuum"
p2.line(x2, y2, color="red")
p2.xaxis.axis_label = "Iterations"
p2.yaxis.axis_label = "Residuum"
p3.line(x3, y3, color="green")
p3.xaxis.axis_label = "Iterations"
p3.yaxis.axis_label = "Residuum"
p4.vbar(x=x4, top=y4, width=0.05, color="Black")
p4.xgrid.grid_line_color = None
show(column(p4, p1, p2, p3))
if __name__ == "__main__":
matrix = define_matrix()
jacobi_method(matrix)
gauss_siedl(matrix)
sor(matrix)
plotting_data()