-
Notifications
You must be signed in to change notification settings - Fork 0
Basic Pendulum
source code: pendulum.py
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, mlatexNext 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:
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:
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]]Contact the developers: symphysicsDev@gmail.com
Symphysics is available under an MIT license Copyright (c) 2020 Robert B., Zeph B.