Numerical solution of first-order differential equations with Python and Tkinter today for you. I wanted to share that with you.

## Differential equations code

import tkinter, math, typing def ti(x, y): return 1 / (math.cos(x)) - y * math.tan(x) # Euler's method def fx(x_0, y_0, x_k, n_1) -> object: h = (x_k - x_0) / n_1 for i in range(0, n_1): y1: typing.Union[float, typing.Any] = y_0 + h * ti(x_0, y_0) x1 = x_0 + h x_0 = x1 y_0 = y1 return y1 def rk(x_0, y_0, x_k, n_1): h = (x_k - x_0) / n_1 #Runge-Kutta 4th Order Method for i in range(0, n_1): k1 = h * ti(x_0, y_0) k2 = h * ti(x_0 + h / 2, y_0 + k1 / 2) k3 = h * ti(x_0 + h / 2, y_0 + k2 / 2) k4 = h * ti(x_0 + h, y_0 + k3) y1: typing.Union[float, typing.Any] = y_0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6 x_0 = x_0 + h y_0 = y1 return y1 def calc_y1(): x0 = float(x0_entry.get()) y0 = float(y0_entry.get()) xk = float(xk_entry.get()) n = int(n_entry.get()) try: y1: str = f'{fx(x0, y0, xk, n):.3f}' y2 = str = f'{rk(x0, y0, xk, n):.3f}' except: y1 = '?' y2 = '?' y1_label.configure(text=y1) y2_label.configure(text=y2) root = tkinter.Tk() frame = tkinter.Frame(root) frame.pack() t1_label = tkinter.Label(frame, bg='tan', text='Numerical solution of first-order differential equations', font='arial 12') t1_label.grid(row=0, column=0, columnspan=5, padx=25, pady=5) x0_entry = tkinter.Entry(frame, width=10) x0_entry.grid(row=1, column=2, pady=5) x0_lebel = tkinter.Label(frame, text='Entry value of X: ') x0_lebel.grid(row=1, column=1, pady=5) y0_entry = tkinter.Entry(frame, width=10) y0_entry.grid(row=2, column=2, pady=5) y0_lebel = tkinter.Label(frame, text='Entry value of Y: ') y0_lebel.grid(row=2, column=1, pady=5) xk_entry = tkinter.Entry(frame, width=10) xk_entry.grid(row=1, column=4, pady=5) xk_lebel = tkinter.Label(frame, text='End value of Х: ') xk_lebel.grid(row=1, column=3, pady=5) n_entry = tkinter.Entry(frame, width=10) n_entry.grid(row=2, column=4, pady=5) n_lebel = tkinter.Label(frame, text='Number of partitions: ') n_lebel.grid(row=2, column=3, pady=5) y1_label = tkinter.Label(frame, text="?") y1_label.grid(row=3, column=1, padx=5, pady=5) y1_lebel = tkinter.Label(frame, text='Euler\'s method: ') y1_lebel.grid(row=4, column=0, padx=5, pady=5) y2_label = tkinter.Label(frame, text='?') y2_label.grid(row=4, column=1, padx=5, pady=5) y2_lebel = tkinter.Label(frame, text='Runge-Kutta 4th Order Method: ') y2_lebel.grid(row=3, column=0, padx=5, pady=5) eval_button = tkinter.Button(frame, bg='coral', text='Evaluate', width=10, command=calc_y1) eval_button.grid(row=4, column=3, padx=5, pady=5) exit_button = tkinter.Button(frame, bg='coral', text='Exit', width=10, command=root.destroy) exit_button.grid(row=4, column=4, padx=5, pady=5) root.mainloop()

There are two methods used: Euler’s and Runge-Kutta.

Enter data: X, Y, expected X and number of partitions.

Click Evaluate to ger the result.

After clicking Exit the GUI will be closed down.