使用nth进行2D插值

时间:2018-08-02 17:24:39

标签: scala math interpolation polynomial-approximations

假设我有2个数组

 x = [1,2,3,4,5,6]
 y = [4,53,234,43,12,2]

适合n的度数y = F(x)的多项式。如何从x内插y

1 个答案:

答案 0 :(得分:2)

一个人可以使用Apache Commons Math。这是一个例子

import scala.collection.JavaConverters._

val x = 1 to 6
val y = Array(4, 53, 234, 43, 12, 2)
val n = 5

val fitter = PolynomialCurveFitter.create(n)
val result = fitter.fit((x zip y).map { case (a, b) =>
  new WeightedObservedPoint(1, a, b)
}.asJava)

println(result.toList)