add function to minimize chi square