Benötige Hilfe beim Code
Verfasst: Samstag 25. Januar 2020, 17:17
Hallo, wir sind 2 Studenten mit fast keine Erfahrung mit Programmieren, und wir versuchen einen Doppelpendel zu animieren.
wir haben es bis jetzt geschafft, das Pendel zu zeichnen und das im Bewegung zu setzten.
Unserer Code hat jedoch einen Fehler in der Animation: die 2D Perspektive ist komprimiert, sodass beim Laufen des Codes die Länge der "Arme" des Pendels sich ändern (es sollte natürlich nicht passieren).
Falls jemand eine Idee hat, wie wir das Problem lösen könnten, wir werden uns sehr freuen
Hier ist unserem Code:
import numpy as np
import matplotlib.pyplot as plt
g=9.81
m1=1
m2=1
l1=1
l2=1
theta1=np.pi/2
theta2=np.pi/2
omega1=0
omega2=0
h=0.005
x1_0=l1*np.sin(theta1)
y1_0=-l1*np.cos(theta1)
x2_0=x1_0+l2*np.sin(theta2)
y2_0=y1_0-l2*np.cos(theta2)
def omega1ddt(theta1,theta2,omega1,omega2):
result=(-g*(2*m1+m2)*np.sin(theta1)-m2*g*np.sin(theta1-2*theta2)-2*np.sin(theta1-theta2)*m2*((omega2**2)*l2+(omega1**2)*l1*np.cos(theta1-theta2)))/(l1*(2*m1+m2-m2*np.cos(2*theta1-2*theta2)))
return result
def omega2ddt(theta1,theta2,omega1,omega2):
result=(2*np.sin(theta1-theta2)*((omega1**2)*l1*(m1+m2)+g*(m1+m2)*np.cos(theta1)+(omega2**2)*l2*m2*np.cos(theta1-theta2))/(l2*(2*m1+m2-m2*np.cos(2*theta1-2*theta2))))
return result
def Runge_kutta(theta1,theta2,omega1,omega2,h):
a1=omega1ddt(theta1, theta2, omega1, omega2)
a2=omega2ddt(theta1, theta2, omega1, omega2)
b1=omega1ddt(theta1, theta2, omega1+(h/2)*a1, omega2+(h/2)*a2)
b2=omega2ddt(theta1, theta2, omega1+(h/2)*a1, omega2+(h/2)*a2)
c1=omega1ddt(theta1, theta2, omega1+(h/2)*b1, omega2+(h/2)*b2)
c2=omega2ddt(theta1, theta2, omega1+(h/2)*b1, omega2+(h/2)*b2)
d1=omega1ddt(theta1, theta2, omega1+(h/1)*c1, omega2+(h/1)*c2)
d2=omega2ddt(theta1, theta2, omega1+(h/1)*c1, omega2+(h/1)*c2)
omega1_n_plus_1=omega1+(h/6)*(a1+2*b1+2*c1+d1)
omega2_n_plus_1=omega2+(h/6)*(a2+2*b2+2*c2+d2)
return omega1_n_plus_1, omega2_n_plus_1
liste_omega1=[omega1]
liste_omega2=[omega2]
liste_theta1=[theta1]
liste_theta2=[theta2]
t=1/24 # in s
i=0
for i in range(5000):
a,b = Runge_kutta(theta1, theta2, omega1, omega2, h)
liste_omega1.append(a)
liste_omega2.append(b)
theta1_new=theta1+a*t
theta2_new=theta2+b*t
liste_theta1.append(theta1_new)
liste_theta2.append(theta2_new)
x1=l1*np.sin(theta1_new)
y1=-l1*np.cos(theta1_new)
x2=x1+l2*np.sin(theta2_new)
y2=y1-l2*np.cos(theta2_new)
theta1=liste_theta1[-1]
theta2=liste_theta2[-1]
omega1=liste_omega1[-1]
omega2=liste_omega2[-1]
#xw1_0=(0, x1_0)
#yw1_0=(0, y1_0)
#xw2_0=(x1_0, x2_0)
#yw2_0=(y1_0, y2_0)
#plt.plot(xw1_0,yw1_0)
#plt.plot(xw2_0,yw2_0)
xw1_1=(0, x1)
yw1_1=(0, y1)
xw2_1=(x1, x2)
yw2_1=(y1, y2)
plt.plot(xw1_1,yw1_1)
plt.plot(xw2_1,yw2_1)
plt.xlim(-3,3)
plt.ylim(-3,3)
#circle1=plt.Circle((x1_0,y1_0),0.02,color='r')
#plt.gcf().gca().add_artist(circle1)
#circle2=plt.Circle((x2_0,y2_0),0.02,color='g')
#plt.gcf().gca().add_artist(circle2)
circle3=plt.Circle((x1,y1),0.02,color='r')
plt.gcf().gca().add_artist(circle3)
circle4=plt.Circle((x2,y2),0.02,color='g')
plt.gcf().gca().add_artist(circle4)
i+=1
plt.pause(0.01)
plt.cla()
wir haben es bis jetzt geschafft, das Pendel zu zeichnen und das im Bewegung zu setzten.
Unserer Code hat jedoch einen Fehler in der Animation: die 2D Perspektive ist komprimiert, sodass beim Laufen des Codes die Länge der "Arme" des Pendels sich ändern (es sollte natürlich nicht passieren).
Falls jemand eine Idee hat, wie wir das Problem lösen könnten, wir werden uns sehr freuen

Hier ist unserem Code:
import numpy as np
import matplotlib.pyplot as plt
g=9.81
m1=1
m2=1
l1=1
l2=1
theta1=np.pi/2
theta2=np.pi/2
omega1=0
omega2=0
h=0.005
x1_0=l1*np.sin(theta1)
y1_0=-l1*np.cos(theta1)
x2_0=x1_0+l2*np.sin(theta2)
y2_0=y1_0-l2*np.cos(theta2)
def omega1ddt(theta1,theta2,omega1,omega2):
result=(-g*(2*m1+m2)*np.sin(theta1)-m2*g*np.sin(theta1-2*theta2)-2*np.sin(theta1-theta2)*m2*((omega2**2)*l2+(omega1**2)*l1*np.cos(theta1-theta2)))/(l1*(2*m1+m2-m2*np.cos(2*theta1-2*theta2)))
return result
def omega2ddt(theta1,theta2,omega1,omega2):
result=(2*np.sin(theta1-theta2)*((omega1**2)*l1*(m1+m2)+g*(m1+m2)*np.cos(theta1)+(omega2**2)*l2*m2*np.cos(theta1-theta2))/(l2*(2*m1+m2-m2*np.cos(2*theta1-2*theta2))))
return result
def Runge_kutta(theta1,theta2,omega1,omega2,h):
a1=omega1ddt(theta1, theta2, omega1, omega2)
a2=omega2ddt(theta1, theta2, omega1, omega2)
b1=omega1ddt(theta1, theta2, omega1+(h/2)*a1, omega2+(h/2)*a2)
b2=omega2ddt(theta1, theta2, omega1+(h/2)*a1, omega2+(h/2)*a2)
c1=omega1ddt(theta1, theta2, omega1+(h/2)*b1, omega2+(h/2)*b2)
c2=omega2ddt(theta1, theta2, omega1+(h/2)*b1, omega2+(h/2)*b2)
d1=omega1ddt(theta1, theta2, omega1+(h/1)*c1, omega2+(h/1)*c2)
d2=omega2ddt(theta1, theta2, omega1+(h/1)*c1, omega2+(h/1)*c2)
omega1_n_plus_1=omega1+(h/6)*(a1+2*b1+2*c1+d1)
omega2_n_plus_1=omega2+(h/6)*(a2+2*b2+2*c2+d2)
return omega1_n_plus_1, omega2_n_plus_1
liste_omega1=[omega1]
liste_omega2=[omega2]
liste_theta1=[theta1]
liste_theta2=[theta2]
t=1/24 # in s
i=0
for i in range(5000):
a,b = Runge_kutta(theta1, theta2, omega1, omega2, h)
liste_omega1.append(a)
liste_omega2.append(b)
theta1_new=theta1+a*t
theta2_new=theta2+b*t
liste_theta1.append(theta1_new)
liste_theta2.append(theta2_new)
x1=l1*np.sin(theta1_new)
y1=-l1*np.cos(theta1_new)
x2=x1+l2*np.sin(theta2_new)
y2=y1-l2*np.cos(theta2_new)
theta1=liste_theta1[-1]
theta2=liste_theta2[-1]
omega1=liste_omega1[-1]
omega2=liste_omega2[-1]
#xw1_0=(0, x1_0)
#yw1_0=(0, y1_0)
#xw2_0=(x1_0, x2_0)
#yw2_0=(y1_0, y2_0)
#plt.plot(xw1_0,yw1_0)
#plt.plot(xw2_0,yw2_0)
xw1_1=(0, x1)
yw1_1=(0, y1)
xw2_1=(x1, x2)
yw2_1=(y1, y2)
plt.plot(xw1_1,yw1_1)
plt.plot(xw2_1,yw2_1)
plt.xlim(-3,3)
plt.ylim(-3,3)
#circle1=plt.Circle((x1_0,y1_0),0.02,color='r')
#plt.gcf().gca().add_artist(circle1)
#circle2=plt.Circle((x2_0,y2_0),0.02,color='g')
#plt.gcf().gca().add_artist(circle2)
circle3=plt.Circle((x1,y1),0.02,color='r')
plt.gcf().gca().add_artist(circle3)
circle4=plt.Circle((x2,y2),0.02,color='g')
plt.gcf().gca().add_artist(circle4)
i+=1
plt.pause(0.01)
plt.cla()