Javascript中的多项式插值法

时间:2019-06-26 03:16:53

标签: javascript interpolation polynomials

我正在尝试对曲线进行多项式插值。 我可以得到3个简单点的简单结果, 但我希望整个曲线都有更好的结果。

我是菜鸟。感谢你的帮助! 我的代码:

var list_dots = [6, 4.5, 6, 4.5, 6, 7.5, 6, 4.5, 3, 1.5, 0, 0, 0, 3, 4.5, 6, 7.5, 6, 7.5, 6, 4.5, 6, 4.5, 3, 1.5, 0, 0, 0, 0, 3, 1.5, 3, 4.5, 3, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1.5, 0, 0, 3, 1.5, 0, 0, 0, 3, 1.5, 0, 3, 1.5, 0, 0, 0, 0, 0, 3, 1.5, 0, 0, 0, 0, 0, 0, 0, 3, 4.5, 3, 1.5, 3, 1.5, 0, 0, 0, 0, 3, 4.5, 3, 4.5, 6, 4.5, 3, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1.5, 0, 3, 1.5, 0, 0, 0, 0, 0, 0, 3, 1.5, 3, 1.5, 0, 0, 0, 0, 0, 3, 1.5, 3, 1.5, 3, 1.5, 3, 1.5, 0, 3, ];

//list_dots
base.beginPath();
base.lineWidth = 2;
base.strokeStyle = '#aaa';
for (var i = 0; i < list_dots.length; i++) {
  base.lineTo(i * 10, (size_canvas_y / 2) - (list_dots[i] * 10));
}
base.stroke();
base.closePath();

//date line interpolation
base.beginPath();
base.lineWidth = 2;
base.strokeStyle = '#F00';
base.moveTo(0, size_canvas_y / 2);
for (var i = 0; i < list_dots.length; i++) {
  if (i > 5) {
    var x = i;
    var x1 = i - 1;
    var x2 = i - 2;
    var x3 = i - 3;
    var y1 = list_dots[i - 1];
    var y2 = list_dots[i - 2];
    var y3 = list_dots[i - 3];
    var y = ((x - x2) * (x - x3)) / ((x1 - x2) * (x1 - x3)) * y1 +
      ((x - x1) * (x - x3)) / ((x2 - x1) * (x2 - x3)) * y2 +
      ((x - x1) * (x - x2)) / ((x3 - x1) * (x3 - x2)) * y3;
  }

  base.lineTo(i * 10, (size_canvas_y / 2) - y * 10);
}

base.stroke();
base.closePath();

我也尝试过此代码:

var xp = 1 ;
var yp = 1 ;
for ( var i=1 ; i<list_dots.length ; i+=10) 
    {
    var p = 1 ;
    for ( var j=1 ; j<list_dots.length ; j+=10) 
        {
        if ( i != j )
            {
            p = p * ( (i - list_dots[j]) / (list_dots[i] - list_dots[j]) ) ;
            }
        }
        yp = yp + (p * list_dots[i]) ;
    base.lineTo(         i*10       ,          (size_canvas_y/2) - yp*10               );
    }

0 个答案:

没有答案