ich arbeite derzeit mit Enthought Canopy zur Animierung einfachster numerischer Lösungen von Differentialgleichungen.
Dabei nimmt im Programmiertext der ganze Kram zur Animation viel mehr Raum ein als die eigentliche Lösungsalgorithmus. Aus diesem Grunde will ich dies in ein Modul schreiben.
Es ist mir bereits gelungen sämtliche die Animation betreffenden Teile in eine Funktion zu schreiben. Quelltext folgend:
Code: Alles auswählen
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math as mat
def ANI2D(X,Y,P1): #P1 ist d
def simData():
i=0
while i < t1/P1:
xa = X[i]
ya = Y[i]
i = i+100
yield xa, ya
def simPoints(simData):
x = simData[0]
y = simData[1]
line1.set_data(x, y)
return line1
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot([], [], 'ro')
ax.set_ylim(min(x), max(x))
ax.set_xlim(min(T), max(T))
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,
interval=1, repeat=True)
#ani.save('HarmonischerOszillator.mp4', fps=15)
return (plt.show())
x = [1]
u = 1
v = 0
D = 1
g = 0
m = 1
a = - D/m *u - g/m *v
t0 = 0
t1 = 5
d = 0.001
T = [0]
t = 0
while t <= t1:
u = u + v*d
v = v + a*d
a = - D/m *u - g/m *v
x = x + [u]
t = t + d
T = T + [t]
ANI2D(T,x,d)
Bitte keine Verbesserungsvorschläge für das Verfahren als solches!
Wie gesagt, es funktioniert einwandfrei. Wenn ich nun aber die Definition der Funktion in ein extra Modul schreibe Animation2d.py.
Code: Alles auswählen
############### Animation2d.py
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math as mat
def ANI2D(X,Y,P1): #P1 ist d
def simData():
i=0
while i < t1/P1:
xa = X[i]
ya = Y[i]
i = i+100
yield xa, ya
def simPoints(simData):
x = simData[0]
y = simData[1]
line1.set_data(x, y)
return line1
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot([], [], 'ro')
ax.set_ylim(min(x), max(x))
ax.set_xlim(min(T), max(T))
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,
interval=1, repeat=True)
#ani.save('HarmonischerOszillator.mp4', fps=15)
return (plt.show())
Code: Alles auswählen
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math as mat
import Animation2d as A2d
x = [1]
u = 1
v = 0
D = 1
g = 0
m = 1
a = - D/m *u - g/m *v
t0 = 0
t1 = 5
d = 0.001
T = [0]
t = 0
while t <= t1:
u = u + v*d
v = v + a*d
a = - D/m *u - g/m *v
x = x + [u]
t = t + d
T = T + [t]
A2d.ANI2D(T,x,d)
Ich habe bereits "einfach" Module erfolgreich anwenden können, aber immer wenn ich in den Modulen selbst Bibliotheken lade bzw. Funktionen aus anderen Bibliotheken verwende kommt dieser Fehler.
Kann mir vielleicht jemand weiterhelfen?
Vielen Dank!
Buddy