I'm looking for how to mathematically relate X and Y, in such a way that the Chi-Squared per degree of freedom is minimised. However I can't understand how this would work, given that I'm trying to use known X,Y dX and dY (errors) values, to get the related gradient and intercept for the line of best fit. nb: I'm currently trying to program a straight line in java that accounts for known errors in both X and Y, however everything I find either obtains errors, or is way over complicated. Please help!!