[Math] Converting 2nd order differential equation to state space

ordinary differential equations

I've got two 2nd order differential equations that I need to convert to state space in order to express them as first order ODE's to model something in Matlab (using ODE45, for what it's worth). They are:

$$\ddot \theta= \frac{-2 \dot r \dot \theta}{r} -\frac {g \cdot \sin(\theta)}{r} – \frac{B}{m} \sqrt{\dot r^2 + r^2\dot\theta^2} $$

$$ \ddot r = g\cdot \cos(\theta) – \frac{k}{m}(r-R)- \frac{B\dot r}{m}\sqrt{\dot r^2 + r^2\dot\theta^2} $$

$B$, $m$, $k$, $g$, and $R$ are constants.

How I've attempted to do this:
$$
\begin{bmatrix}
r_1\\
r_2\\
\end{bmatrix} = \begin{bmatrix}
r\\
\dot r\\
\end{bmatrix}
$$
$$
\begin{bmatrix}
\theta_1\\
\theta_2\\
\end{bmatrix} = \begin{bmatrix}
\theta\\
\dot \theta\\
\end{bmatrix}
$$
Taking the derivatives of these, I got:
$$\begin{bmatrix}
\dot r_1\\
\dot r_2\\
\end{bmatrix} = \begin{bmatrix}
\dot r\\
\ddot r\\
\end{bmatrix}
$$
$$
\begin{bmatrix}
\dot \theta_1\\
\dot \theta_2\\
\end{bmatrix} = \begin{bmatrix}
\dot \theta\\
\ddot \theta\\
\end{bmatrix}
$$

So,
$$
\begin{bmatrix}
\dot \theta_1\\
\dot \theta_2\\
\dot r_1\\
\dot r_2\\
\end{bmatrix} = \begin{bmatrix}
\dot \theta\\
\frac{-2 \dot r \dot \theta}{r} -\frac {g \cdot \sin(\theta)}{r} – \frac{B}{m} \sqrt{\dot r^2 + r^2\dot\theta^2} \\
\dot r\\
g\cdot \cos(\theta) – \frac{k}{m}(r-R)- \frac{B\dot r}{m}\sqrt{\dot r^2 + r^2\dot\theta^2} \\
\end{bmatrix}
$$

This may be the right answer, but when I provide initial conditions and attempt to model this in Matlab, I get a wacky looking plot that doesn't make much sense.

Did I go wrong anywhere in here? Thanks in advance for any and all help.

Best Answer

On the right hand side you have $\theta$, $\dot\theta$, $r$ and $\dot r$. They should be $\theta_1$, $\theta_2$, $r_1$ and $r_2$.

Related Question