def METHOD_NAME(self, x, derivatives=0):
    r"""
    Returns the residual
    .. math::
        0.5 (\texttt{label} - \texttt{labeltarget})^2
    and optionally the gradient wrt surface params.
    """
    assert derivatives in [0, 1]
    self.surface.x = x
    l = self.label.J()
    rl = l - self.targetlabel
    val = 0.5 * rl ** 2
    if derivatives:
        dl = self.label.dJ_by_dsurfacecoefficients()
        dval = rl * dl
        return val, dval
    else:
        return val