pykoop.dynamic_models.Pendulum

class Pendulum(mass, length, damping=0)

Bases: ContinuousDynamicModel

Point-mass pendulum with optional damping.

State is [angle, angular_velocity].

Examples

Simulate a pendulum

>>> pend = pykoop.dynamic_models.Pendulum(0.5, 1, 0.6)
>>> x0 = np.array([np.pi / 2, 0])
>>> t, x = pend.simulate((0, 1), 1e-3, x0, lambda t: 0)
__init__(mass, length, damping=0)

Instantiate Pendulum.

Parameters:
  • mass (float) – Mass (kg).

  • length (float) – Length (m).

  • damping (float) – Viscous damping (N.m.s/rad).

Methods

__init__(mass, length[, damping])

Instantiate Pendulum.

f(t, x, u)

Implement differential equation.

g(t, x)

Implement output equation.

simulate(t_range, t_step, x0, u, **kwargs)

Simulate the model using numerical integration.

f(t, x, u)

Implement differential equation.

Parameters:
  • t (float) – Time (s).

  • x (np.ndarray) – State.

  • u (np.ndarray) – Input.

Returns:

Time derivative of state.

Return type:

np.ndarray

g(t, x)

Implement output equation.

Parameters:
  • t (float) – Time (s).

  • x (np.ndarray) – State.

Returns:

Measurement of state.

Return type:

np.ndarray

simulate(t_range, t_step, x0, u, **kwargs)

Simulate the model using numerical integration.

Parameters:
  • t_range (Tuple[float, float]) – Start and stop times in a tuple.

  • t_step (float) – Timestep of output data.

  • x0 (np.ndarray) – Initial condition, shape (n, ).

  • u (Callable[[float], np.ndarray]) – Input function of time.

  • **kwargs (dict) – Keyword arguments for integrate.solve_ivp().

Returns:

Time and state at every timestep. Each timestep is one row.

Return type:

Tuple[np.ndarray, np.ndarray]