本文介绍了将 n 个点逼近具有最佳拟合的曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 n 个点的列表(2D):P1(x0,y0), P2(x1,y1), P3(x2,y2) ...点满足每个点都有唯一坐标的条件,并且每个点的坐标xi,yi> 0 和xi,yi 都是整数.

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

  • 到曲线y = |Acos (Bx) | 最佳拟合(接近或等于 100%)
  • 因此系数 A 和 B 尽可能简单.
  • to the curve 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.

我想用 C# 编写程序,但对我来说最大的问题是找到合适的算法.有没有人能帮我解决这个问题?

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?

推荐答案

基于 近似搜索的工作原理我会尝试在 C++ 中:

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

它使用上面链接的问题中我的 approx

It uses my approx class from the question linked above

  • 您需要设置min,maxstep 范围以匹配您的数据集
  • 可以通过增加递归次数来提高准确性
  • 可以根据需要提高性能
    • 并非所有点都用于不太准确的递归层
    • 增加起始步长(但如果太大则会使结果无效)
    • 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)

      您还应该添加输入点和输出曲线的图,以查看是否接近解决方案.如果没有关于输入点的更多信息,就很难更具体.您可以更改差异计算 e 以匹配任何需要的方法,这只是 abs 差异的总和(可以使用最小二乘法或任何...)

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

      这篇关于将 n 个点逼近具有最佳拟合的曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:17