# Least squares fit to a straight line?

1. Jan 30, 2010

### noname1

I was wondering if someone could explain how to compute the Least squares fit to a straight line

2. Jan 30, 2010

### ehild

You have N experimental points (xi,yi) and you want to fit a straight line y=ax+b across them so that the mean value of the square of the deviations y(xi)-yi is minimum with respect to the parameters a and b
.
$$S=\sum_1^N{(ax_i+b-y_i)^2} = minimum$$

For that, the partial derivatives of S have to be zero. This condition yields two equations for a and b.

$$\partial S /\partial a=\sum_1^N{2(ax_i+b-y_i)x_i}=0$$
$$\partial S /\partial b=\sum_1^N{2(ax_i+b-y_i)}=0$$

Rearranging the equations:

$$a\sum_1^N{x_i^2}+b\sum_1^N{x_i}=\sum_1^N{x_i y_i}$$

$$a\sum_1^N{x_i}+N b=\sum_1^N{y_i}$$

Solve for a and b.

ehild

3. Jan 30, 2010

### noname1

i have solved it and got this

y = 46.3x+(-61.8)

my question is now, i plotted my points on a table on paper but how do i make the straight line?

4. Jan 31, 2010

### ehild

Just calculate two points of your equation, put them onto the plot and connect them with a straight line :)

ehild

5. Jan 31, 2010

### noname1

how didnt i think of that duhhh lol, one more question now how do i calculate g from the slope?