gsl akima样条的插值误差

时间:2014-12-09 17:20:55

标签: c interpolation gsl

我正在使用以下代码获得'gsl:interp.c:150:错误:插值错误'。一些谷歌搜索说,当你尝试使用interp函数进行推断时会发生这种错误,但我不知道这是怎么回事。非常感谢帮助。感谢。

函数randomground()只返回一个随机数(double)。

#define NSTEPS 100   

int main()
{
           int j, q, space = 1, refine = 100;
           double xi = 0.0, tx[2*NSTEPS] = {0}, theight[2*NSTEPS] = {0};

           double terrain[(int) (2*NSTEPS*100)] = {0};
           double terrainsl[(int) (2*NSTEPS*100)] = {0};

            for (j = 0; j < 2*NSTEPS; j++)
            {
                tx[j] = (double) j*space;
                theight[j] = randomground();
            }

            gsl_interp_accel *acc = gsl_interp_accel_alloc();
            gsl_spline *spline = gsl_spline_alloc(gsl_interp_akima, 2*NSTEPS);
            gsl_spline_init(spline, tx, theight, 2*NSTEPS);

            for (q = 0; q< 2*NSTEPS*100; q++)
            {
                terrain[q] = gsl_spline_eval(spline,xi,acc);
                terrainsl[q] = gsl_spline_eval_deriv(spline,xi,acc);
                xi = xi+(double) space/refine;
            }
return 0;
}

3 个答案:

答案 0 :(得分:1)

通过向tx和theight添加额外元素来解决问题。我猜这是你要我做的,@ ViniciusMiranda。代码现在读取

                double tx[2*NSTEPS+1] = {0}, theight[2*NSTEPS+1] = {0};
                double terrain[(int) (2*NSTEPS*100)] = {0};
                double terrainsl[(int) (2*NSTEPS*100)] = {0};

            for (j = 0; j < 2*NSTEPS+1; j++)
            {
                tx[j] = (double) j*space;
                theight[j] = randomground();
            }

            gsl_interp_accel *acc = gsl_interp_accel_alloc();
            gsl_spline *spline = gsl_spline_alloc(gsl_interp_akima, 2*NSTEPS+1);
            gsl_spline_init(spline, tx, theight, 2*NSTEPS+1);

            for (q = 0; q< 2*NSTEPS*100; q++)
            {
                terrain[q] = gsl_spline_eval(spline,xi,acc);
                terrainsl[q] = gsl_spline_eval_deriv(spline,xi,acc);
                xi = xi+(double) space/refine;
            }

我仍然不明白为什么这个修复是必需的。

答案 1 :(得分:0)

Akima样条曲线是局部的子样条插值。对于函数f(x),如果您要为x获取x_i <= x <= x_i+1的值,则Akima样条线需要(x_j, f_j)的对j = i-2, i-1, i, i+1, i+2, i+3.

答案 2 :(得分:0)

我在代码zigzag.sourceforge.net中发现了相同的错误,并通过注释gsl library source中的行,对其进行编译并重新安装来解决。

  

在版本1.14之前,interp.c中的第150行

// GSL_ERROR_VAL("interpolation error", GSL_EDOM, GSL_NAN);

我发表评论以解决中断问题!