Approximation of n points to the curve with the best fit

后端 未结 2 419
闹比i
闹比i 2020-12-21 22:16

I have a list of n points(2D): P1(x0,y0), P2(x1,y1), P3(x2,y2) … Points satisfy the condition that each point has unique coordinates and also the coordinates of each point

相关标签:
2条回答
  • 2020-12-21 23:06

    Based on How approximation search works I would try this in C++:

    // (global) input data
    #define _n 100
    double px[_n]; // x input points
    double py[_n]; // y input points
    
    // approximation
    int ix;
    double e;
    approx aa,ab;
    //            min  max   step  recursions  ErrorOfSolutionVariable
    for (aa.init(-100,+100.0,10.00,3,&e);!aa.done;aa.step())
    for (ab.init(-0.1,+  0.1, 0.01,3,&e);!ab.done;ab.step())
        {
        for (e=0.0,ix=0;ix<_n;ix++) // test all measured points (e is cumulative error)
            {
            e+=fabs(fabs(aa.a*cos(ab.a*px[ix]))-py[ix]);
            }
        }
    // here aa.a,ab.a holds the result A,B coefficients
    

    It uses my approx class from the question linked above

    • you need to set the min,max and step ranges to match your datasets
    • can increase accuracy by increasing the recursions number
    • can improve performance if needed by
      • using not all points for less accurate recursion layers
      • increasing starting step (but if too big then it can invalidate result)

    You should also add a plot of your input points and the output curve to see if you are close to solution. Without more info about the input points it is hard to be more specific. You can change the difference computation e to match any needed approach this is just sum of abs differences (can use least squares or what ever ...)

    0 讨论(0)
  • 2020-12-21 23:10

    Taking B as an independent parameter, you can solve the fitting for A using least-squares, and compute the fitting residual.

    The residue function is complex, with numerous minima of different value, and an irregular behavior. Anyway, if the Xi are integer, the function is periodic, with a period related to the LCM of the Xi.

    The plots below show the fitting residue for B varying from 0 to 2 and from 0 to 10, with the given sample points.

    enter image description here enter image description here

    0 讨论(0)
提交回复
热议问题