# curve correction by new point

6 views (last 30 days)
Commented: Yugal Gupta on 30 May 2017
Hi, There is a curve that for every 5 seconds, a new point is obtained for correcting the curve. By "polyfit" command, it has to start from first stage and consider all points (before points and the new point). I want to consider only the new point affection (correction the curve). In other word, for saving time I need to consider only the curve and new point for obtaining new corrected curve and not need to consider before points and new point to 'polyfit' them. can anybody help me?
Star Strider on 23 Dec 2014
You might if you search the Internet. I wrote the code for it in FORTRAN about 30 years ago, and those files are long gone.
Yugal Gupta on 30 May 2017
Do You have written code for your problem,"Curve correction by new point "? Actually, the same problem I have to handle. Can you help me?

John D'Errico on 23 Dec 2014
Edited: John D'Errico on 23 Dec 2014
This something you could do with a QR update or insert. There is a qrinsert code in MATLAB.
However, you will find that polyfit is pretty fast, and that unless you have thousands of points, that the update will be a waste of time. Note that just doing an update still means you will need to write the code for an updated solution. So you will need to essentially write polyfit anyway.
You would do as well you just write your own simple polyfit code, that does not do the extra computations that polyfit does. The time gained there will be far more significant than what you will gain from the qr update.
The point is, if you don't need the other stuff that polyfit generates and returns, then not computing it will save you time. And a simplified polyfit is trivial to write.
For example, if k is the order polynomial you will fit, and x and y are vectors of the same length...
simplepolyfit = @(x,y,k) bsxfun(@power,x(:),k:-1:0)\y(:);
So it does no error checking. It does not return R^2, or any other parameters of interest.
x = randn(1000,1);
y = rand(1000,1);
polyfit(x,y,1)
ans =
-0.0019974 0.50881
simplepolyfit(x,y,1)
ans =
-0.0019974
0.50881
The two codes return the same numbers. And the simple version does use a nicely numerically stable routine to compute the solution. If it matters, changing simplepolyfit to return a row vector is a trivial thing to do, so it would be completely consistent with polyfit. This will do it:
simplepolyfit = @(x,y,k) (bsxfun(@power,x(:),k:-1:0)\y(:))';
How about the time for these codes to run?
timeit(@() simplepolyfit(x,y,1))
ans =
0.0001349
timeit(@() polyfit(x,y,1))
ans =
0.00091916
But on the above simple example, it ran 7 times faster than polyfit, a significant gain in time.
Or you can write a function that will also compute a few minor additional parameters, like residual errors, etc, and still see a gain in time.
really thanks John, but i must write this code in C++ and number of points are too many. so i cant consider previous points to update the curve
John D'Errico on 23 Dec 2014
Well, then it is time for you to learn how to fit a polynomial to data, and it is time for you to learn tools such as a qr update, which will allow you to do the computations efficiently. You have not even told us the order of polynomial you will be fitting, so I can't help you more. You will be able to find some of the linear algebra tools you will need in the C version of Lapack. Do some reading.

Yugal Gupta on 30 May 2017
Do You have written code for your problem,"Curve correction by new point "? Actually, the same problem I have to handle. Can you help me?