Comparison of two methods for the calculation of the gradient and of the Hessian of cost functions associated with differential systems