doublePendulum#
- class peccary.examples.doublePendulum(L1=1.0, L2=1.0, M1=1.0, M2=1.0)[source]#
Bases:
objectThe
doublePendulumclass 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, andM2. To initialize the class with the defaults, use:>>> import peccary.examples as ex >>> pendSys = ex.doublePendulum()
The
integratemethod allows for control of the timestep resolution (default \(2^{-6}\)), the time to integrate overtDur, 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.Timeseriesclass in thexandyattributes 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
doublePendulumclass:plotStaticandplotAnimate. The methodplotStaticplots 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 themassargument tomass=1. The method returnsmatplotlib.figure.Figureandmatplotlib.axes.Axesobjects, which can be plotted or saved to a file. To plot, call the method for the initialized system and use the integratedTimeseriesfor the parametertser, i.e.,>>> fig,ax = pendSys.plotStatic(pend) >>> plt.show() # to show plot >>> ### OR ### >>> plt.savefig('pend.png') # to save plot
The
plotAnimatemethod 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
Timeseriesclass created by doublePendulum.integrate()
- Returns:
matplotlib.lines.Line2DobjectLines representing the pendulum rods
matplotlib.lines.Line2DobjectPoints representing history
matplotlib.text.TextinstanceText 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
Timeseriesclass 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
Timeseriesclass created by doublePendulum.integrate()- massint, optional
Pendulum mass to plot, by default 2 (lower mass)
- Returns:
- fig
matplotlib.figure.Figure matplotlibfigure created for plot- axarray of
matplotlib.axes.Axes Array of
matplotlibAxes created for plot
- fig