SymPy teaser

Here's something I did recently with sympy: figure out the Jacobian matrix of the $\dot{\tau}$ control law.


In [1]:
from sympy import *
init_printing()

State variables:

  • s: displacement
  • v: velocity
  • d: deceleration

Parameters:

  • b: stiffness

In [2]:
s, v, d, b = symbols('s, v, d, b')

In [3]:
state_variables = Matrix([s, v, d])
state_variables


Out[3]:
$$\left[\begin{matrix}s\\v\\d\end{matrix}\right]$$

In [4]:
def tau_dot(s, v, d):
    return s * d / v**2 - 1

In [5]:
d_deceleration = - b * (tau_dot(s, v, d) - (-1/2))
d_deceleration


Out[5]:
$$- b \left(\frac{d s}{v^{2}} - 0.5\right)$$

In [6]:
full_model = Matrix([
    - v,
    - d,
    d_deceleration
])
full_model


Out[6]:
$$\left[\begin{matrix}- v\\- d\\- b \left(\frac{d s}{v^{2}} - 0.5\right)\end{matrix}\right]$$

In [9]:
jac =full_model.jacobian(state_variables)
jac


Out[9]:
$$\left[\begin{matrix}0 & -1 & 0\\0 & 0 & -1\\- \frac{b d}{v^{2}} & \frac{2 b}{v^{3}} d s & - \frac{b s}{v^{2}}\end{matrix}\right]$$

In [ ]: