implement the cost function, but not working yet