정보통신기술(ICT)

상미분 방정식: 로렌츠 방정식(Lorenz Equations)

해머슴 2024. 11. 4. 13:47
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk

# 로렌츠 방정식 정의
def lorenz(X, t, sigma, beta, rho):
    x, y, z = X
    dx_dt = sigma * (y - x)
    dy_dt = x * (rho - z) - y
    dz_dt = x * y - beta * z
    return [dx_dt, dy_dt, dz_dt]

# 그래프 그리기 함수
def plot_lorenz(sigma, beta, rho, x0, y0, z0):
    t = np.linspace(0, 50, 10000)
    initial_state = [x0, y0, z0]
    solution = odeint(lorenz, initial_state, t, args=(sigma, beta, rho))

    fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
    ax.plot(solution[:, 0], solution[:, 1], solution[:, 2], lw=0.5)
    ax.set_title("Lorenz Attractor")
    ax.set_xlabel("X Axis")
    ax.set_ylabel("Y Axis")
    ax.set_zlabel("Z Axis")

    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.draw()
    canvas.get_tk_widget().pack()

# 버튼 클릭 이벤트
def on_submit():
    try:
        sigma = float(entry_sigma.get())
        beta = float(entry_beta.get())
        rho = float(entry_rho.get())
        x0 = float(entry_x0.get())
        y0 = float(entry_y0.get())
        z0 = float(entry_z0.get())
        plot_lorenz(sigma, beta, rho, x0, y0, z0)
    except ValueError:
        tk.messagebox.showerror("입력 오류", "올바른 숫자를 입력해주세요.")

# GUI 설정
window = tk.Tk()
window.title("Lorenz Equations Solver")

tk.Label(window, text="Sigma (σ):").pack()
entry_sigma = tk.Entry(window)
entry_sigma.pack()
tk.Label(window, text="Beta (β):").pack()
entry_beta = tk.Entry(window)
entry_beta.pack()
tk.Label(window, text="Rho (ρ):").pack()
entry_rho = tk.Entry(window)
entry_rho.pack()
tk.Label(window, text="Initial x:").pack()
entry_x0 = tk.Entry(window)
entry_x0.pack()
tk.Label(window, text="Initial y:").pack()
entry_y0 = tk.Entry(window)
entry_y0.pack()
tk.Label(window, text="Initial z:").pack()
entry_z0 = tk.Entry(window)
entry_z0.pack()

submit_button = tk.Button(window, text="Plot Lorenz Attractor", command=on_submit)
submit_button.pack()

window.mainloop()

 

 

https://www.tistory.com/event/write-challenge-2024

 

작심삼주 오블완 챌린지

오늘 블로그 완료! 21일 동안 매일 블로그에 글 쓰고 글력을 키워보세요.

www.tistory.com