Created
January 1, 2016 04:47
-
-
Save brandonpelfrey/6077035205cf18142484 to your computer and use it in GitHub Desktop.
Automatically Deriving the Angular Accelerations for N-Pendula
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sympy import * | |
import matplotlib.pyplot as plt | |
# time variable | |
t = symbols('t') | |
# number of pendula + 1 | |
Z = 3 + 1 | |
# Constants | |
G = symbols('G') | |
M = symbols('M0:%s' % Z) | |
L = symbols('L0:%s' % Z) | |
# Angles and their first and second derivatives | |
u = symbols('u0:%s' % Z) | |
v = symbols('v0:%s' % Z) | |
w = symbols('w0:%s' % Z) | |
# cartesian coordinates | |
x, y = [0], [0] | |
for i in xrange(1,Z): | |
x.append( x[-1] + L[i] * cos( u[i](t) ) ) | |
y.append( y[-1] + L[i] * sin( u[i](t) ) ) | |
# velocity in cartesian coordinates | |
K = 0 | |
for i in xrange(1,Z): | |
K += M[i] * (diff(x[i], t) ** 2 + diff(y[i], t) ** 2 ) / 2 | |
K = simplify( K ) | |
P = 0 | |
for i in xrange(1,Z): | |
P += M[i] * G * y[i] | |
L = K - P | |
print L | |
EL = [0] | |
for i in xrange(1,Z): | |
EL.append( L.diff( Derivative(u[i](t), t) ).diff(t) - L.diff(u[i](t) ) ) | |
S = {} | |
for i in xrange(1,Z): | |
S[Function(u[i].name)(t)] = u[i] | |
S[Derivative(Function(u[i].name)(t), t)] = v[i] | |
S[Derivative(Function(u[i].name)(t), t, t)] = w[i] | |
for i in xrange(1,Z): | |
EL[i] = EL[i].subs(S) | |
first_order_odes = solve(EL[1:], w[1:]) | |
Q = [0] | |
for i in xrange(1,Z): | |
Q.append( simplify(first_order_odes[w[i]]) ) | |
things_to_replace = 'uvML' | |
for thing in things_to_replace: | |
for i in xrange(1,Z): | |
for j in xrange(1,Z): | |
Q[j] = str(Q[j]).replace('%s%s' % (thing, i), '%s[%s]' % (thing, i)) | |
for i in xrange(1,Z): | |
print 'Q%s' % i | |
print Q[i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Angular Accelerations for Triple Pendulum (i.e. d_tt theta_i(t)). u == theta_i, v = d/dt u, w = d/dt v .