doublePendulum#

class peccary.examples.doublePendulum(L1=1.0, L2=1.0, M1=1.0, M2=1.0)[source]#

Bases: object

The doublePendulum class can be used to generate the x- and y-timeseries of a specified double pendulum system.

References

[1] Code based on a Matplotlib tutorial

[2] The formulae in that tutorial turn were translated from the C code by Michael S. Wheatland

Examples

By default, the lengths of the pendulum rods are 1 meter and the two masses are 1 kilogram each, but these can be changed using the parameters L1, L2, M1, and M2. To initialize the class with the defaults, use:

>>> import peccary.examples as ex
>>> pendSys = ex.doublePendulum()

The integrate method allows for control of the timestep resolution (default \(2^{-6}\)), the time to integrate over tDur, and the initial conditions (\(\theta_1=120^\circ\), \(\omega_1=0^\circ ~\text{s}^{-1}\), \(\theta_2=-10^\circ\), \(\omega_1=0^\circ ~\text{s}^{-1}\)). To integrate the systems for 10.0 seconds with the default resolution and initial conditions, use:

>>> pend = pendSys.integrate(tDur=10.0)

The x- and y- coordinates are stored in a peccary.timeseries.Timeseries class in the x and y attributes in (2,n)-shape arrays. For example, to index the x-coordinates of mass 2, use:

>>> pend.x[1]

There are also two plotting methods built in to the doublePendulum class: plotStatic and plotAnimate. The method plotStatic plots the xy-plane of the pendulum system, as well as the x- and y-timeseires of the specified pendulum mass. By default, it plots the lower mass, mass 2, but mass 1 can be selected setting the mass argument to mass=1. The method returns matplotlib.figure.Figure and matplotlib.axes.Axes objects, which can be plotted or saved to a file. To plot, call the method for the initialized system and use the integrated Timeseries for the parameter tser, i.e.,

>>> fig,ax = pendSys.plotStatic(pend)
>>> plt.show() # to show plot
>>> ### OR ###
>>> plt.savefig('pend.png') # to save plot

The plotAnimate method works the same way, except that it animates the xy-plane only. It can be used as follows:

>>> pendSys.plotAnimate(pend)

Methods Summary

animateFrame(i, tser)

Animate a frame of the double pendulum system

derivs(t, state)

Get partial derivatives for double pendulum system

integrate([tDur, dt, th1, w1, th2, w2])

Integrate double pendulum system

plotAnimate(tser)

Animate a integrated double pendulum system

plotStatic(tser[, mass])

Quick-plot y(x), x(t), and y(t) for one of the masses of a double pendulum system.

Methods Documentation

animateFrame(i, tser)[source]#

Animate a frame of the double pendulum system

Parameters:
iint

Index of the frame to animate

tserpeccary.timeseries.Timeseries

Timeseries class created by doublePendulum.integrate()

Returns:
matplotlib.lines.Line2D object

Lines representing the pendulum rods

matplotlib.lines.Line2D object

Points representing history

matplotlib.text.Text instance

Text object of current frame timestep

Notes

This will very rarely be used alone; please use the plotAnimate function instead, which is a wrapper for this function

derivs(t, state)[source]#

Get partial derivatives for double pendulum system

Parameters:
tndarray

Time array

state_type_

Initial conditions

Returns:
ndarray

Partial derivatives

integrate(tDur=2.5, dt=np.float64(0.015625), th1=120.0, w1=0.0, th2=-10.0, w2=0.0)[source]#

Integrate double pendulum system

Parameters:
tDurfloat, optional

How many seconds to simulate, by default 2.5

dtfloat, optional

Time resolution in seconds, by default 0.015625

th1float, optional

Initial angle of mass 1 in degrees, by default 120.0*u.deg

w1float, optional

Initial angular velocity of mass 1 in degrees/second, by default 0.0

th2float, optional

Initial angle of mass 2 in degrees, by default -10.0*u.deg

w2float, optional

Initial angular velocity of mass 2 in degrees/second, by default 0.0

Returns:
ndarray

Timeseries of x-coordinates for mass 1

ndarray

Timeseries of y-coordinates for mass 1

ndarray

Timeseries of x-coordinates for mass 2

ndarray

Timeseries of y-coordinates for mass 2

plotAnimate(tser)[source]#

Animate a integrated double pendulum system

Parameters:
tserpeccary.timeseries.Timeseries

Timeseries class created by doublePendulum.integrate()

plotStatic(tser, mass=2)[source]#

Quick-plot y(x), x(t), and y(t) for one of the masses of a double pendulum system.

Parameters:
tserpeccary.timeseries.Timeseries

Timeseries class created by doublePendulum.integrate()

massint, optional

Pendulum mass to plot, by default 2 (lower mass)

Returns:
figmatplotlib.figure.Figure

matplotlib figure created for plot

axarray of matplotlib.axes.Axes

Array of matplotlib Axes created for plot