简体   繁体   中英

Approximation of n points to the curve with the best fit

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 xi, yi> 0 and xi,yi are integers.

The task is to write an algorithm which make approximation of these points

  • to the curve y = | Acos (Bx) | y = | Acos (Bx) | with the best fit (close or equal to 100%)
  • and so that the coefficients A and B were as simple as possible.

I would like to write a program in C # but the biggest problem for me is to find a suitable algorithm. Has anyone would be able to help me with this?

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.

在此处输入图片说明在此处输入图片说明

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 ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM