Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.2 kB
2
Indexable
Never
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg,
    NavigationToolbar2Tk
)
from matplotlib.figure import Figure
from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

from matplotlib.widgets import TextBox
from matplotlib.widgets import Button

import tkinter as tk
import matplotlib

matplotlib.use('TkAgg')


x0 = 3
y0 = 3
z0 = 2
vx0 = -1
vy0 = 5
vz0 = 0

t0 = 0
tf = 2

m = np.array([1, 0, 0])
r = np.array([3, 3, 2])
Q = 1
a = 1


class Sim:

    def mag_bewegung(t, D, m, Q, r, a):
        x, vx, y, vy, z, vz = D
        v = np.array([vx, vy, vz])
        o = np.array([x, y, z])
        ra = o - r
        rar = np.sqrt(sum(ra*ra))
        B = (3 * (np.dot(m, (ra)/rar))*((ra)/rar)-m)/rar*rar*rar
        s = (Q*np.cross(v, B))/a
        sx = s[0]
        sy = s[1]
        sz = s[2]
        fun = [vx, sx, vy, sy, vz, sz]
        return fun

    def plotme(mag_bewegung):
        D0 = [x0, y0, z0, vx0, vy0, vz0]
        NumSol = solve_ivp(mag_bewegung, [t0, tf], D0, method="RK45", args=(
            m, Q, r, a), atol=1e-8, rtol=1e-8)
        aa, bb, cc, dd, ee, ff = NumSol.y
        t = NumSol.t
        ax = plt.figure().add_subplot(projection='3d')

        # create the barchart
        ax.plot(aa, bb, cc)
        ax.set_xlabel('X-Koordinate')
        ax.set_ylabel('Y-Koordinate')
        ax.set_zlabel('Z-Koordinate')

    plotme()


def updatePlot():
    Sim.plotme()
    app.update()


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title('Tkinter Matplotlib Demo')

        # create a figure
        figure = Figure(figsize=(6, 4), dpi=100)

        # create FigureCanvasTkAgg object
        figure_canvas = FigureCanvasTkAgg(figure, self)

        # create the toolbar
        NavigationToolbar2Tk(figure_canvas, self)

        figure_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        Startknopf = tk.Button(text="Start", command=Sim.plotme())
        Startknopf.pack()

        MagnetX = tk.Entry()
        MagnetX.pack()


if __name__ == '__main__':
    app = App()
    app.mainloop()