solve cost and gradient for logistic regression