Skip to content

Basic Pendulum

rjbourne edited this page Jun 9, 2020 · 2 revisions

Basic Pendulum

In this example we will use SystemL to create a basic pendulum

First we make the necessary imports

import symphysics
import sympy as sp
import numpy as np
from sympy.physics.mechanics import dynamicsymbols, mlatex

Next we create the system, here we will use the coordinate giving angle from equilibrium

m, g, l = sp.symbols('m, g, l') #constants
a= dynamicsymbols('theta') #coordinates
coords = [a] # put coordinates in list
L = m*l**2*a.diff()**2/2 + m*g*l*sp.cos(a)

Pendulum = symphysics.SystemL(L, coords)

We can now print out the equations of motion by accessing the motion attribute

print(Pendulum.motion)
print(mlatex(Pendulum.motion))

which outputs

>>>[Eq(Derivative(omega_theta(t), t), -g*sin(theta(t))/l)]
>>>\left[ \dot{\omega}_{\theta} = - \frac{g \operatorname{sin}\left(\theta\right)}{l}\right]

The first of these is the sympy object, the second is a latex respresentation of it:

equation

Note the use of omega_theta for the second derivative of theta with respect to time - SystemL creates all equations of motion as first order ODEs. You can access the full list with the attribute motion1, whch will also give us the trivial ODE:

equation

Finally we can collect data for the pendulum by setting some constants and initial conditions, as well as a range of times:

#numerical values of constants
consts = [(m, 1), (l, 1), (g, 9.81)]
#initial conditions - 1 radian of rotation with no initial angular velocity
initials = [1, 0]
#times at whch to collect data
times = np.linspace(0,5,5) #every second for 5 seconds
data = Pendulum.ODESolve(initials, times, consts)
print(data)

which outputs

[[ 1.          0.        ]
 [-0.86762951  1.44439542]
 [ 0.4971966  -2.57754542]
 [ 0.01954696  3.00258549]
 [-0.53034726 -2.51477589]]

Clone this wiki locally