## Least squares fitting where we pre-determine the slope

### Barbara Wortham (view profile)

on 16 May 2018
Latest activity Commented on by Ameer Hamza

on 13 Aug 2018

### Ameer Hamza (view profile)

Hello all,
I have a set of data, say on a scatter plot, and I have a line that has a pre-determined slope (i.e. not determined by the data on the scatter plot but determined by ideal calculations). I would like to move the line through the data until it has the least amount of offset (like least-squares fitting) but without changing the slope of the line. Is there a least-squares fitting function that lets you pre-determine the slope?
Cheers!

### Ameer Hamza (view profile)

on 16 May 2018
Edited by Ameer Hamza

### Ameer Hamza (view profile)

on 16 May 2018

Yes, you can do that following the pattern of the linear regression fitting with little modification. In linear regression, we fit the equation
a*x+b = y
and in MATLAB we write it as
[x_vector ones(size(x_vector))]\y_vector
to get a and b. But since you already know slope a, your equation become
b = y-a*x
so in MATLAB use
ones(size(x_vector))\[y_vector-a*x_vector]
it will give you the value of b which minimize the least square error.

Barbara Wortham

on 17 May 2018
Thanks everyone!