You are on page 1of 3

04.02.

23, 20:18 22W_TM3_MB-CED: Python-Code für die Animation des Doppelpendels

Technische Mechanik III (22W)



Painel / Meus cursos / 22W_TM3_MB-CED / Numerik und Tools / Python-Code für die Animation des Doppelpendels

Python-Code für die Animation des Doppelpendels


import sys
from scipy.integrate import odeint
from math import pi, sin, cos
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

s1 = 0.5
d1 = 1.0
s2 = 0.5
d2 = 1.0
k1 = 0.5*s1
k2 = 0.5*s2
m1 = 1.0
m2 = 1.0
g = 9.81

z0 = [1.02*0.8*pi,0.0,-0.0*pi,0.0]
N=600
tend = 10.0
toutput = np.linspace(0.0,tend,N)

def appellpendel(a,z,t):
    phi1tt = a[0]
    phi2tt = a[1]
    phi1 = z[0]
    phi1t = z[1]
    phi2 = z[2]
    phi2t = z[3]

    a1x = -s1*sin(phi1)*phi1t**2 + s1*cos(phi1)*phi1tt


    a1y = -s1*cos(phi1)*phi1t**2 - s1*sin(phi1)*phi1tt
    a2x = -d1*sin(phi1)*phi1t**2 + d1*cos(phi1)*phi1tt \
        -s2*sin(phi2)*phi2t**2 + s2*cos(phi2)*phi2tt
    a2y = -d1*cos(phi1)*phi1t**2 - d1*sin(phi1)*phi1tt \
        -s2*cos(phi2)*phi2t**2 - s2*sin(phi2)*phi2tt
    return 0.5*m1*(a1x**2 + a1y**2 + k1**2*phi1tt**2) +\
        0.5*m2*(a2x**2 + a2y**2 + k2**2*phi2tt**2)

def rhspendel(z,t):
    phi1 = z[0]
    phi1t = z[1]
    phi2 = z[2]
    phi2t = z[3]

    c = appellpendel([0,0],z,t)
    S1 = appellpendel([1,0],z,t)
    S2 = appellpendel([-1,0],z,t)
    a11 = 0.5*(S1 + S2) - c
    b1 = 0.5*(S1-S2)
    S1 = appellpendel([0,1],z,t)

https://lms.bht-berlin.de/mod/page/view.php?id=929278 1/3
04.02.23, 20:18 22W_TM3_MB-CED: Python-Code für die Animation des Doppelpendels

    S2 = appellpendel([0,-1],z,t)
    a22 = 0.5*(S1 + S2) - c
    b2 = 0.5*(S1-S2)    
    a12 = appellpendel([1,1],z,t) -a11 - a22 - b1 - b2 - c

    Q1 = -(m1*s1 + m2*d1)*g*sin(phi1)


    Q2 = -m2*s2*g*sin(phi2)
    Delta = 4.0*a11*a22 - a12**2
    phi1tt = ((Q1-b1)*2.0*a22 - (Q2-b2)*a12)/Delta
    phi2tt = ((Q2-b2)*2.0*a11 - (Q1-b1)*a12)/Delta
    return [phi1t,phi1tt,phi2t,phi2tt]

ergebnis = odeint(rhspendel,z0,toutput)

phi1_tab = ergebnis[ : , 0]
omega1_tab = ergebnis[ : , 1]
phi2_tab = ergebnis[ : , 2]
omega2_tab = ergebnis[ : , 3]

xD1 = d1*np.sin(phi1_tab)
yD1 = -d1*np.cos(phi1_tab)
xD2 = xD1 + d2*np.sin(phi2_tab)
yD2 = yD1 - d2*np.cos(phi2_tab)

plt.figure(1)
plt.plot(xD1, yD1,"b-",linewidth=2)
plt.plot(xD2, yD2,"r--")
plt.axis('equal')
plt.xlabel("$x$")
plt.ylabel("$y$")
plt.savefig('tm3-dy-doppelpendel-D1D2_2.pdf')
#plt.show()

#Animation
fig = plt.figure(2)
ax = fig.add_subplot(
    111,
    autoscale_on=False,
    xlim=(-d1-d2,d1+d2),
    ylim=(-d1-d2, d1+d2),
    aspect='equal'
)
ax.axhline(color='0.6', linestyle='dashed')

line1, = ax.plot([], [], 'ko-', lw=4)


line2, = ax.plot([], [], 'r--', lw=1)

def animate(i):
    x = [0,xD1[i],xD2[i]]
    y = [0,yD1[i],yD2[i]]
    line1.set_data(x, y)
    line2.set_data(xD2[0:i],yD2[0:i])
    return line1, line2

plt.tight_layout()
if 'save' in sys.argv:
    ani = animation.FuncAnimation(fig, animate, range(N), interval=25)
    ani.save('doppelpendel_var2.gif', dpi=80, writer='pillow') #ani.save('kurbelschwinge.gif', dpi=80, writer='imagemagick')
else:
    ani = animation.FuncAnimation(fig, animate, range(N), interval=30,repeat_delay=2000)
    plt.show()

https://lms.bht-berlin.de/mod/page/view.php?id=929278 2/3
04.02.23, 20:18 22W_TM3_MB-CED: Python-Code für die Animation des Doppelpendels

Última atualização: Sonntag, 19 Dez 2021, 19:40

◄ Notizen zur numerischen Lösung des AWP in Python

Seguir para...

Julia-Code für die Lösung des Schwingers aus Aufgabe 10.3 ►

Português - Brasil (‎pt_br)‎


Deutsch (‎de)‎
English (‎en)‎
Español - Internacional ‎(es)‎
Français (‎fr)‎
Italiano ‎(it)‎
Polski ‎(pl)‎
Português - Brasil (‎pt_br)‎
Português - Portugal ‎(pt)‎
Türkçe ‎(tr)‎
Русский ‎(ru)‎
‫ العربية‬‎(ar)‎

Obter o aplicativo para dispositivos móveis

Berliner Hochschule für Technik | Datenschutz- und Einwilligungserklärung | Hinweise zum Urheberrecht | Impressum

https://lms.bht-berlin.de/mod/page/view.php?id=929278 3/3

You might also like