ex2: cost function and gradient