Skip to content
Carlos Adir edited this page Nov 15, 2021 · 2 revisions

To start, please see in the page RBDyn problem what kind of problem we treat.

To define a simple pendulum, we have

from matplotlib import pyplot as plt
from compmec.rbdyn import *

a = 1  # Rope's lenght or radius
theta = Variable("theta")  # The angle
R0 = FrameReference()  # The Inertial Reference Frame
R1 = FrameReference(R0, rotation=(-pi/2 + theta, "z"))
R2 = FrameReference(R1, translation=(a, 0, 0))
ball = Object(R2, name="ball")  # We put the ball connected to the frame R2
ball.mass = 1  # Pendulum's mass
E = ball.KineticEnergy(R0)  # We get the kinetic energy

theta0 = pi/3  # Initial position at t = 0
dtheta0 = 0  # Initial velocity at t = 0
Simulation.initialconditions = {theta: (theta0, dtheta0)}
Simulation.totaltime = 10  # We are going to simulate 10 seconds
Simulation.totalenergy = E
results = Simulation.run()

times = results["time"]  # or the variable time
thetas = results["theta"]  # or the variable theta
plt.plot(times, thetas)
plt.show()