ex2: Completed costFunction and gradient