On 25 May 2010, at 14:20, Oisín Ó Cuanacháin wrote:
Hi,
Does anyone know if there is a function to fit a line with a
specified slope to some data. I have a load of data 'x' and
polyfit(1:length(x),x,1) gives me the best-fit line, with arbitrary
slope.
What I want however is the best fit line with a particular slope.
This is essentially just a minimization problem and I know I could
code something up myself, but x has 100's of millions of elements
I'm guessing my coding efforts will take forever to run, so does
such a nice function already exist? It is essentially polyfit with
an added constraint that the slope is fixed.
Thanks,
Oisín.
If I understand correctly, given two data vectors x and y and a
slope a you want to find the value b that minimizes
R(b) = sum ((a*x + b - y).^2);
isn't that just
b = sum (a*x-y)/2