  A fundamental challenge in learning an unknown dynamical system is to reduce
model uncertainty by making measurements while maintaining safety. We formulate
a mathematical definition of what it means to safely learn a dynamical system
by sequentially deciding where to initialize trajectories. The state of the
system must stay within a safety region for a horizon of $T$ time steps under
the action of all dynamical systems that (i) belong to a given initial
uncertainty set, and (ii) are consistent with information gathered so far.
  First, we consider safely learning a linear dynamical system involving $n$
states. For the case $T=1$, we present an LP-based algorithm that either safely
recovers the true dynamics from at most $n$ trajectories, or certifies that
safe learning is impossible. For $T=2$, we give an SDP representation of the
set of safe initial conditions and show that $\lceil n/2 \rceil$ trajectories
generically suffice for safe learning. For $T = \infty$, we provide
SDP-representable inner approximations of the set of safe initial conditions
and show that one trajectory generically suffices for safe learning. We extend
a number of our results to the cases where the initial uncertainty set contains
sparse, low-rank, or permutation matrices, or when the system has a control
input.
  Second, we consider safely learning a general class of nonlinear dynamical
systems. For the case $T=1$, we give an SOCP-based representation of the set of
safe initial conditions. For $T=\infty$, we provide semidefinite representable
inner approximations to the set of safe initial conditions. We show how one can
safely collect trajectories and fit a polynomial model of the nonlinear
dynamics that is consistent with the initial uncertainty set and best agrees
with the observations. We also present some extensions to cases where the
measurements are noisy or the dynamical system involves disturbances.
