how to find min and max of a function