regularized cost function and gradient