Approximation of 2D-function with weight coefficients
I need to approximate a table-defined 2D-function like that
x0 y0
x1 y1
...
xn yn
for every point I have a "weight" (root-mean-square error for this measure). I need to write a function like this:
typedef std::vector< double > DVector;
void approxi开发者_运维问答mate2D(
const DVector & x
, const DVector & y
, const DVector & weights
, double newMeasuredX
, double newMeasuredY
, double newMeasuredWeight
, double & outApproximatedX
, double & outApproximatedY
);
to get one value ( outApproximatedX; outApproximatedY ) depend on previous values and new measured value.
Root-mean-square (RMS) error should be used as follows: if a RMS error is minimal, then desired function should go close to this point, if a RMS error is maximal, then this point should be use with a minimal contribution.
Approximation should be linear (I think), since I know, that desired function is a straight line.
Googled about a half of a day, and did not found any suggestions.
Thank you.
To minimize the total squared closest distances to the line a x + b y = r, you can't use matrix equations, since the problem is no longer linear.
The distance to the line can be defined as follows. Then the function you want to minimize is f(a,b,r). This task is simplified somewhat when a2 + b2 = 1.
If you expand that, it gets quite complex. I managed to break it down and simplify it somewhat.
To calculate this over many points (O(n2)) can get slow. There is however an easy optimization. Instead of calculating the sums over and over again, you can store partial results:
Here the σ variables are accumulators for common terms. Each time you want to add another point to the calculations, you update the 9 variables, and use those to calculate a, b and r as before.
What you want is a space-filling-curve either a Hilbert-Curve or a Peano-Curve. A sfc is a good approximation of a 2D or XD grid.
Thanks a lot for MizardX. A great help. That is what I wrote ( if someone need :) )
http://liveworkspace.org/code/815e2cc0810ab8ef14951252cca3fbbf
P.S. I can't vote for MizardX (no reputation). Can somebody do it for me ?
精彩评论