Completed the cost function and the gradient calculation...