Skip to content

Pythoneo

Online How to Python stuff

Numerical solution of first-order differential equations

Posted on December 27, 2020June 4, 2022 By Luke K

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

differential equations calculated

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 the number of partitions.
Click Evaluate to get the result.
After clicking Exit, the GUI will be closed down.

See also  Here's how to draw a hyperbola
math, Tkinter Tags:equation, Euler, Runge-Kutta

Post navigation

Previous Post: Tkinter GUI with side menu
Next Post: How to create wheel?

Categories

  • bokeh (1)
  • datetime (3)
  • Django (1)
  • glob (1)
  • io (1)
  • json (1)
  • math (4)
  • matplotlib (9)
  • numpy (84)
  • OpenCV (1)
  • os (3)
  • Pandas (2)
  • pathlib (2)
  • Pillow (3)
  • Plotly (1)
  • Python (25)
  • random (7)
  • requests (1)
  • Scipy (4)
  • Seaborn (3)
  • shutil (1)
  • sqlite3 (1)
  • statistics (16)
  • sys (1)
  • Tkinter (9)
  • turtle (2)
  • urllib (1)
  • webbrowser (1)

RSS RSS

  • How to use curdoc in Bokeh
  • How to solve TypeError: ‘set’ object is not subscriptable
  • How to calculate the factorial of an array in Numpy?
  • How to Make a Countplot in Seaborn
  • AttributeError: partially initialized module ‘cv2’ has no attribute ‘img’ (most likely due to a circular import)
  • How to enumerate dictionary in Python?
  • How to compare two arrays in Numpy?
  • How to square a matrix in Numpy?
  • Exploding out slices of a Pie Chart in Plotly
  • How to uninstall Numpy?

Tags

arithmetic mean array axis button calculations chart conversion copy count counter data type dictionary dimension draw error files fill float generate grid GUI image index integer list matrix max mean median min normal distribution plot random reshape rotate round size standard deviation string sum test text time variance zero

Copyright © 2023 Pythoneo.

Powered by PressBook WordPress theme

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT