我知道这是一个稍微“请做我的功课”的问题,但我正在尝试实施De Boor算法,如下所述:http://en.wikipedia.org/wiki/De_Boor's_algorithm
我有一个泛型类,它接受任何类型的任何数组和两个函数,它们为每个点产生x和y。
当我向GetPoint(x)提供非常接近_d中最低x值的x时,我得到非常不准确的插值。
是否有人能够看到我做错了什么?非常感谢提前。
我正在使用的数据是:
x y
0.002739726 0.999988548
0.019178082 0.999917365
0.038356164 0.999835128
0.057534247 0.999753381
0.082191781 0.999648832
0.167123288 0.999286638
0.249315068 0.998939303
0.334246575 0.998583164
0.419178082 0.998222171
0.495890411 0.997899634
0.747945205 0.99680615
1 0.99559971
1.249315068 0.994187653
1.495890411 0.9926708
1.747945205 0.99066351
2 0.988439344
2.249315068 0.985377424
2.498630137 0.981972695
2.750684932 0.978188863
3.002739726 0.974069647
4.002739726 0.952415995
5.002739726 0.926226504
6.002739726 0.897271422
7.005479452 0.866241091
8.005479452 0.834348616
9.005479452 0.802369725
代码
public class Spline<T>
{
/// <summary>
/// Coordinate
/// </summary>
private struct TwoDPoint
{
public double x;
public double y;
};
/// <summary>
/// Unused apart from debugging.
/// </summary>
private T [] _originalDataPoints;
/// <summary>
/// Stores the points we will perform the algorithm on.
/// </summary>
private TwoDPoint [] _d;
private int _n; // Value for the "order" used in the De Boor algorithm
public Spline(T [] dataPoints, Func<T, double> xFunc, Func<T, double> yFunc, int n)
{
SortAndAssignPoints(dataPoints, xFunc, yFunc);
_n = n;
}
/// <summary>
/// Populates points in d sorted ascending by their x value
/// </summary>
private void SortAndAssignPoints(T [] dataPoints, Func<T, double> xFunc, Func<T, double> yFunc)
{
_originalDataPoints = dataPoints;
_d = dataPoints
.Select(point => new TwoDPoint()
{
x = xFunc(point),
y = yFunc(point)
})
.OrderBy(point => point.x).ToArray();
}
/// <summary>
/// Function which gets an interpolated value
/// </summary>
public double GetPoint(double x)
{
double sum = 0;
for (int i = 0; i < _d.Length; i++)
{
sum += _d[i].y * N_General(_n, i, x);
}
return sum;
}
/// <summary>
/// General N function as given in the algorithm
/// </summary>
private double N_General(int n, int i, double x)
{
if (n == 0)
{
return N_Base(i, x);
}
double firstCoefficient;
if ((i + n) > (_d.Length - 1))
{
firstCoefficient = 0D;
}
else
{
firstCoefficient =
(x - _d[i].x) / (_d[i + n].x - _d[i].x);
}
double secondCoefficient;
if ((i + n + 1) > (_d.Length - 1))
{
secondCoefficient = 0D;
}
else
{
secondCoefficient =
(_d[i + n + 1].x - x) / (_d[i + n + 1].x - _d[i + 1].x);
}
return firstCoefficient * N_General(n - 1, i, x) +
secondCoefficient * N_General(n - 1, i + 1, x);
}
/// <summary>
/// N_i^0 as given in the algorithm
/// </summary>
private double N_Base(int i, double x)
{
//Bound check
if (
(i >= (_d.Length - 1)) ||
(i < 0)
)
{
return 0;
}
if ((x >= _d[i].x) &&
(x < _d[i+1].x))
{
return 1;
}
else
{
return 0;
}
}
}
答案 0 :(得分:2)
从您的代码中,您似乎使用_d中的y值作为控制点,使用_d中的x值作为结值。请注意,对于B样条曲线,控制点通常不在曲线本身上。因此,您不应期望从数据数组传递x值到GetPoint(x)函数将获取数据数组中相应的y值。
此外,对于B样条曲线,应始终遵循“结数=控制点数+顺序”的规则。你真的不能使用_d [i] .x作为节点而_d [i] .y作为控制点,因为你将拥有相同数量的节点和控制点。
最后,我建议使用this作为参考,而不是维基百科链接,因为它提供了对De Boor-Cox算法的更好描述。