finished ex1 - cost function