-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
292 lines (237 loc) · 9.88 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import time
from tkinter import *
from tkinter import messagebox
from numpy import *
import numerical_methods
# Opens results in new windows after pressing GO
def open_new_window():
# Toplevel object which will
# be treated as a new window
new_window = Toplevel(root)
# sets the title of the
# Toplevel widget
new_window.title("Iterations")
# sets the geometry of toplevel
new_window.state('zoomed')
# A Label widget to show in toplevel
# Label(newWindow,text="This is a new window").pack()
return new_window
# Handling of bracketing methods and creates table iterations
def start_bracketing_method(method):
f = lambda x: eval(f_x_entry.get().replace("^", "**"))
xl = float(xl_entry.get())
xu = float(xu_entry.get())
es = float(es_entry.get())
imax = int(imax_entry.get())
if method == "Bisection":
begin = time.time()
res_ar = numerical_methods.bisection(xl, xu, f, es, imax)
end = time.time()
else:
begin = time.time()
res_ar = numerical_methods.false_position(xl, xu, f, es, imax)
end = time.time()
if res_ar is None:
messagebox.showwarning("showwarning", "Warning: No root found")
return
nw = open_new_window()
font = ('Arial', 14, 'bold')
xl_lbl = Label(nw, text="i", font=font)
xl_lbl.grid(row=0, column=0)
xl_lbl = Label(nw, text="xl", font=font)
xl_lbl.grid(row=0, column=1)
fxl_lbl = Label(nw, text="f(xl)", font=font)
fxl_lbl.grid(row=0, column=2)
xu_lbl = Label(nw, text="xu", font=font)
xu_lbl.grid(row=0, column=3)
fxu_lbl = Label(nw, text="f(xu)", font=font)
fxu_lbl.grid(row=0, column=4)
xr_lbl = Label(nw, text="xr", font=font)
xr_lbl.grid(row=0, column=5)
fxr_lbl = Label(nw, text="f(xr)", font=font)
fxr_lbl.grid(row=0, column=6)
ea_lbl = Label(nw, text="ea", font=font)
ea_lbl.grid(row=0, column=7)
# code for creating table
i = 0
j = 0
for i in range(len(res_ar)):
for j in range(len(res_ar[0])):
e = Entry(nw, width=10, fg='blue', font=('Arial', 16, 'bold'))
e.grid(row=i + 1, column=j)
e.insert(END, res_ar[i][j])
root_lbl = Label(nw, text=f"Root = {res_ar[i][5]}", font=font)
root_lbl.grid(row=0, column=j + 1)
time_lbl = Label(nw, text=f"Time = {str(end - begin)} s", font=font)
time_lbl.grid(row=1, column=j + 1)
# Handling of the open methods and creates table iterations
def start_open_method(method):
f = lambda x: eval(f_x_entry.get().replace("^", "**"))
es = float(es_entry.get())
imax = int(imax_entry.get())
if method == "Fixed point":
x0 = float(x0_entry.get())
g = lambda x: eval(g_x_entry.get().replace("^", "**"))
begin = time.time()
res_ar = numerical_methods.fixed_point(x0, g, es, imax)
end = time.time()
elif method == "Newton-Raphson":
f = f_x_entry.get().replace("^", "**")
xi = float(xi_entry.get())
begin = time.time()
res_ar = numerical_methods.newton_raphson(xi, f, es, imax)
end = time.time()
elif method == "Secant":
x_iminus1 = float(x_iminus1_entry.get())
xi = float(xi_entry.get())
begin = time.time()
res_ar = numerical_methods.secant(x_iminus1, xi, f, es, imax)
end = time.time()
nw = open_new_window()
font = ('Arial', 14, 'bold')
i_lbl = Label(nw, text="i", font=font)
i_lbl.grid(row=0, column=0)
x_iminus1_lbl = Label(nw, text="x i-1", font=font)
x_iminus1_lbl.grid(row=0, column=1)
xi_lbl = Label(nw, text="xi", font=font)
xi_lbl.grid(row=0, column=2)
xi_lbl = Label(nw, text="f(x i-1)", font=font)
xi_lbl.grid(row=0, column=3)
xi_lbl = Label(nw, text="f(xi)", font=font)
xi_lbl.grid(row=0, column=4)
xu_lbl = Label(nw, text="ea", font=font)
xu_lbl.grid(row=0, column=5)
if method == "Fixed point" or method == "Newton-Raphson":
nw = open_new_window()
font = ('Arial', 14, 'bold')
xl_lbl = Label(nw, text="i", font=font)
xl_lbl.grid(row=0, column=0)
xl_lbl = Label(nw, text="xi", font=font)
xl_lbl.grid(row=0, column=1)
fxl_lbl = Label(nw, text="x i+1", font=font)
fxl_lbl.grid(row=0, column=2)
xu_lbl = Label(nw, text="ea", font=font)
xu_lbl.grid(row=0, column=3)
# code for creating table
i = 0
j = 0
for i in range(len(res_ar)):
for j in range(len(res_ar[0])):
e = Entry(nw, width=10, fg='blue', font=('Arial', 16, 'bold'))
e.grid(row=i + 1, column=j)
e.insert(END, res_ar[i][j])
root_lbl = Label(nw, text=f"Root = {res_ar[i][2]}", font=font)
root_lbl.grid(row=0, column=j + 1)
time_lbl = Label(nw, text=f"Time = {str(end - begin)} s", font=font)
time_lbl.grid(row=1, column=j + 1)
# Clicking Go button handling based on method choice
def go_func():
# Replace '^' with '**' for Python interpreter
if clicked.get() == "Bisection" or clicked.get() == "False-position":
start_bracketing_method(
clicked.get()) # # print(xr) # return (xl_entry.get(), xu_entry.get(), f_x_entry.get(), clicked.get(), es_entry.get(), imax_entry.get())
else:
start_open_method(clicked.get())
# Drop down menu method selection handling. Shows method-specific parameters frame
def setMethodName(method_name):
# print(method_name)
global xl_entry, xu_entry, g_x_entry, xi_entry, x0_entry, x_iminus1_entry
clicked.set(method_name)
for widgets in method_frame.winfo_children():
widgets.destroy()
# method_frame.destroy()
# method_frame.
# method_frame = LabelFrame(root, text="Method-specific Parameters")
# method_frame.grid_remove()
# method_frame.grid(row=4, column=0, rowspan=2, columnspan=2, padx=5, pady=5)
if method_name == "Bisection" or method_name == "False-position":
xl_lbl = Label(method_frame, text="xl = ")
xl_entry = Entry(method_frame, width=20)
xu_lbl = Label(method_frame, text="xu = ")
xu_entry = Entry(method_frame, width=20)
xl_lbl.grid(row=0, column=0, padx=5, pady=5)
xl_entry.grid(row=0, column=1, padx=5, pady=5)
xu_lbl.grid(row=1, column=0, padx=5, pady=5)
xu_entry.grid(row=1, column=1, padx=5, pady=5)
elif clicked.get() == "Fixed point":
g_x_lbl = Label(method_frame, text="g(x) = ")
g_x_entry = Entry(method_frame, width=20)
x0_lbl = Label(method_frame, text="x0 = ")
x0_entry = Entry(method_frame, width=20)
g_x_lbl.grid(row=0, column=0, padx=5, pady=5)
g_x_entry.grid(row=0, column=1, padx=5, pady=5)
x0_lbl.grid(row=1, column=0, padx=5, pady=5)
x0_entry.grid(row=1, column=1, padx=5, pady=5)
elif clicked.get() == "Newton-Raphson":
xi_lbl = Label(method_frame, text="xi = ")
xi_entry = Entry(method_frame, width=20)
xi_lbl.grid(row=0, column=0, padx=5, pady=5)
xi_entry.grid(row=0, column=1, padx=5, pady=5)
elif clicked.get() == "Secant":
x_iminus1_lbl = Label(method_frame, text="x i-1 = ")
x_iminus1_entry = Entry(method_frame, width=20)
xi_lbl = Label(method_frame, text="xi = ")
xi_entry = Entry(method_frame, width=20)
x_iminus1_lbl.grid(row=0, column=0, padx=5, pady=5)
x_iminus1_entry.grid(row=0, column=1, padx=5, pady=5)
xi_lbl.grid(row=1, column=0, padx=5, pady=5)
xi_entry.grid(row=1, column=1, padx=5, pady=5)
# Take input from txt file
def file_opener():
res = ''
from tkinter import filedialog
input = filedialog.askopenfile(initialdir="../")
for i in input:
res += i
# x = len(equation.get())
f_x_entry.delete(0, END)
f_x_entry.insert(0, res)
if __name__ == '__main__':
root = Tk()
root.title("Numerical Solver")
root.geometry("480x360")
root.resizable(False, False)
p1 = PhotoImage(file='assets/icon.png')
root.iconphoto(False, p1)
# Labels
f_x_lbl = Label(root, text="f(x) = ")
method_lbl = Label(root, text="Method")
es_lbl = Label(root, text="es")
imax_lbl = Label(root, text="Max Iterations")
# Text boxes
f_x_entry = Entry(root, width=35, borderwidth=3)
es_entry = Entry(root, width=20)
es_entry.insert(0, "0.00001")
imax_entry = Entry(root, width=20)
imax_entry.insert(0, "50")
# Drop-down Menu
options = ["Bisection", "False-position", "Fixed point", "Newton-Raphson", "Secant"]
clicked = StringVar()
clicked.set("")
methods_dropdown = OptionMenu(root, clicked, *options, command=setMethodName)
methods_dropdown.config(width=20)
# Buttons
reset_btn = Button(root, text="Reset", width=20)
clear_results_btn = Button(root, text="Clear Results", width=20)
single_step_btn = Button(root, text="Single-step", width=20)
go_btn = Button(root, text="Go", width=20, command=go_func)
f_x_entry_btn = Button(root, text="Browse File", command=file_opener)
# Frames
method_frame = LabelFrame(root, text="Method-specific Parameters")
# Root Grid
f_x_lbl.grid(row=0, column=0, padx=5, pady=5)
f_x_entry.grid(row=0, column=1, padx=5, pady=5)
# equation.grid(row=0, column=3, padx=5, pady=5)
f_x_entry_btn.grid(row=0, column=2, padx=5, pady=5)
method_frame.grid(row=4, column=0, rowspan=2, columnspan=2, padx=5, pady=5)
method_lbl.grid(row=1, column=0, padx=5, pady=5)
methods_dropdown.grid(row=1, column=1, padx=5, pady=5)
es_lbl.grid(row=2, column=0, padx=5, pady=5)
es_entry.grid(row=2, column=1, padx=5, pady=5)
imax_lbl.grid(row=3, column=0, padx=5, pady=5)
imax_entry.grid(row=3, column=1, padx=5, pady=5)
reset_btn.grid(row=7, column=0, padx=5, pady=5)
clear_results_btn.grid(row=7, column=1, padx=5, pady=5)
single_step_btn.grid(row=8, column=0, padx=5, pady=5)
go_btn.grid(row=8, column=1, padx=5, pady=5)
root.mainloop()