# Straight Line Fit

The java applet below calculates the best fit of your data to a straight line represented by the equation $$y = m x + b$$ where $$m$$ is the slope and $$b$$ is the intercept. It takes the errors in both $$x$$ and $$y$$ into account and calculates the error in both the slope and the intercept. If you turn off $$x$$ and $$y$$ errors below, you will get the same slope and intercept value that you get from Excel’s trendline.

To find the best fit of your data, simply copy and paste whole columns or rows directly from Excel into the corresponding boxes below and click the calculate button.

##### Straight Line Fit Calculator
 $${\large x}$$ $${\large \delta x}$$ $${\large y}$$ $${\large \delta y}$$ 1 1 2 1 2 1 4 2 3 1 7 1 4 1 9 2 5 1 11 1
 include $$x$$-error include $$y$$-error Set Y intercept 0 $$y = m x + b$$ $$m \quad = \quad$$ $$\quad \pm \quad$$ $$b \quad = \quad$$ $$\quad \pm \quad$$ $$\chi^2$$ per degree of freedom $$\quad = \quad$$

##### How does it work?

Let us first start with the mathematical description of a straight line. As we mentioned in the beginning of the page a straight line is represented by the equation $$y = mx +b,$$ where $$m$$ is the slope and $$b$$ is the intercept. To be able to quantify how good a particular line fits our data, we need to define a function that takes the slope and the intercept of our line and returns a numerical goodness of fit.

Our calculator uses the so-called $$\chi^2$$ function as a goodness of fit which is defined as $$\chi^2(m,b) = \sum_{i=1}^{n} \frac{\left[y_i – (m x_i + b) \right]^2}{(\delta y_i)^2 + m^2 (\delta x_i)^2},$$ where the sum is over our $$n$$ data points. The numerator is a measure of the $$y$$ distance (or its square) between a data point and our line. The denominator weights each data point based on the error in $$x$$ and $$y$$ values.

So for every possible straight line, our $$\chi^2$$ function gives us a value that is a measure of how good that particular line fits our data. A small $$\chi^2$$ value means a good fit and a large $$\chi^2$$ value means a bad fit. So the only thing we need to do is to minimize this function.

It turns out this is not very easy to do for the most general case. It can be done analytically (or we can write down a formula) if we don’t include the error in $$x$$ and $$y$$ or if we only include the error in $$y$$. For the general case, where we include the measurement errors for both $$x$$ and $$y$$, we have to do a numerical minimization. Our calculator finds an approximate solution to this minimization problem by using an iterative method.