简体   繁体   English

拟合最接近曲线的n个点

[英]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. 我有n个点(2D)的列表:P1(x0,y0),P2(x1,y1),P3(x2,y2)…点满足每个点具有唯一坐标以及每个点xi的坐标的条件,yi> 0并且xi,yi是整数。

The task is to write an algorithm which make approximation of these points 任务是编写一个算法来近似这些点

  • to the curve y = | Acos (Bx) | 到曲线y = | Acos (Bx) | y = | Acos (Bx) | with the best fit (close or equal to 100%) 最合适(接近或等于100%)
  • and so that the coefficients A and B were as simple as possible. 从而使系数A和B尽可能简单。

I would like to write a program in C # but the biggest problem for me is to find a suitable algorithm. 我想用C#编写程序,但是对我来说最大的问题是找到合适的算法。 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. B作为独立参数,可以使用最小二乘法求解A的拟合,并计算拟合残差。

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 . 无论如何,如果Xi是整数,则该函数是周期性的,并且周期与XiLCM有关。

The plots below show the fitting residue for B varying from 0 to 2 and from 0 to 10 , with the given sample points. 下图显示了在给定采样点下B的拟合残差在02010

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

Based on How approximation search works I would try this in C++: 基于近似搜索的工作原理,我将在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 它使用我上面链接的问题中的approx

  • you need to set the min,max and step ranges to match your datasets 您需要设置min,maxstep范围以匹配数据集
  • 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 ...) 您可以更改差值计算e来匹配任何所需的方法,这只是绝对差值的总和(可以使用最小二乘或其他形式...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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