Matched layers are commonly used in numerical simulations of wave propagation to model (semi-)infinite domains. Attenuation functions describe the damping in layers, and provide a matching of the wave impedance at the interface between the domain of interest and the absorbing region. Selecting parameters in the attenuation functions is non-trivial. In this work, an optimisation procedure for automatically calibrating matched layers is presented. The procedure is based on solving optimisation problems constrained by partial differential equations with polynomial and piecewise-constant attenuation functions. We show experimentally that, for finite element time domain simulations, piecewise-constant attenuation function are at least as efficient as quadratic attenuation functions. This observation leads us to introduce consecutive matched layers as an alternative to perfectly matched layers, which can easily be employed for problems with arbitrary geometries. Moreover, the use of consecutive matched layers leads to a reduction in computational cost compared to perfectly matched layers. Examples are presented for acoustic, elastodynamic and electromagnetic problems. Numerical simulations are performed with the libraries FEniCS/DOLFIN and dolfin-adjoint, and the computer code to reproduce all numerical examples is made freely available.