pykoop.dynamic_models.MassSpringDamper

class MassSpringDamper(mass, stiffness, damping)

Bases: ContinuousDynamicModel

Mass-spring-damper model.

State is [position, velocity].

Examples

Simulate a mass-spring-damper

>>> msd = pykoop.dynamic_models.MassSpringDamper(0.5, 0.7, 0.6)
>>> x0 = np.array([1, 0])
>>> t, x = msd.simulate((0, 1), 1e-3, x0, lambda t: 0)
Parameters:
__init__(mass, stiffness, damping)

Instantiate MassSpringDamper.

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

  • stiffness (float) – Stiffness (N/m).

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

Return type:

None

Methods

__init__(mass, stiffness, damping)

Instantiate MassSpringDamper.

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.

Attributes

A

Compute A matrix.

B

Compute B matrix.

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]

property A

Compute A matrix.

property B

Compute B matrix.