finish cost function with regularization