Straight Line Fit
The java applet below calculates the best fit of your data to a straight line represented by the equation where
is the slope and
is the intercept. It takes the errors in both
and
into account and calculates the error in both the slope and the intercept. If you turn off
and
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
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 where
is the slope and
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 function as a goodness of fit which is defined as
where the sum is over our data points. The numerator is a measure of the
distance (or its square) between a data point and our line. The denominator weights each data point based on the error in
and
values.
So for every possible straight line, our function gives us a value that is a measure of how good that particular line fits our data. A small
value means a good fit and a large
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 and
or if we only include the error in
. For the general case, where we include the measurement errors for both
and
, we have to do a numerical minimization. Our calculator finds an approximate solution to this minimization problem by using an iterative method.