Create costFunction