ex2 cost function