Javascript 等价于 Matlab 函数的函数:Polyfit/Polyval?

发布于 2024-11-26 00:09:53 字数 649 浏览 1 评论 0原文

迫切需要一个与 Matlab 中存在的 polyval 和 polyfit 函数等效的 Javascript。本质上,matlab 中的这些函数根据指定的多项式基于两个大小相等的数组进行曲线拟合。我需要做一些涉及 JavaScript 中曲线拟合的计算,但我一生都找不到等效的函数。

这是MatLab对函数polyfit的解释

"P = POLYFIT(X,Y,N) 求多项式 P(X) 的系数 在最小二乘意义上最适合数据 Y 的度 N。 P 是 一个 长度为 N+1 的行向量,包含多项式系数 降幂,P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1)。"

这是MatLab对polyval的解释。

“POLYVAL 计算多项式。 Y = POLYVAL(P,X) 返回在以下位置计算的多项式 P 的值 X.P 是一个长度为 N+1 的向量,其元素是 这 多项式的降幂。

<前><代码> Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)"

任何帮助都会很棒。

问候,

Desperately need a Javascript equivalent to polyval and polyfit functions that exist in Matlab. Essentially those functions in matlab do a curve fit based on two equally sized arrays depending on a specified polynomial. I need to do some calculations that involve curve fitting in javascript and can't for the life of me find an equivalent function.

This is MatLab's explanation of the function polyfit

"P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
degree N that fits the data Y best in a least-squares sense. P is
a
row vector of length N+1 containing the polynomial coefficients in
descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1)."

This is MatLab's explanation of polyval.

"POLYVAL Evaluate polynomial.
Y = POLYVAL(P,X) returns the value of a polynomial P evaluated at
X. P
is a vector of length N+1 whose elements are the coefficients of
the
polynomial in descending powers.

    Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)"

Any help would be super.

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稳稳的幸福 2024-12-03 00:09:53

numericjs 可能会帮助您入门。

numericjs may help you get started.

熟人话多 2024-12-03 00:09:53

POLYFIT 执行最小二乘 多项式拟合归结为求解线性方程组。我进行了快速搜索,但找不到基本的 线性代数 Javascript 库解决此类系统...最简单的方法是实现 高斯自行消除算法。

POLYVAL 只是通过代入方程中的系数来计算 X 点处的多项式。

POLYFIT performs a least-square polynomial fitting which comes down to solving a system of linear equations. I did a quick search, but I couldn't find a basic linear algebra Javascript library that solves such systems... The easiest method would be to implement the Gaussian elimination algorithm yourself.

POLYVAL is simply evaluating the polynomial at the points X by substituting the coefficients in the equation.

别闹i 2024-12-03 00:09:53

也许这段代码可以帮助某人

function _prepare(_mat) {
_mat=[[]].concat(_mat)
for(i=0;i<_mat.length;++i)
    _mat[i]=[0].concat(_mat[i])
return _mat
}

function linear(_mat){
_mat=_prepare(_mat)
return _solve(_mat)
}


function _solve(_mat){
var c=new Array(),d=new Array()
var n=_mat.length-1

for(i=0;i<=n+1;i++) {
    d[i]=new Array();
    c[i]=0
    for(j=0;j<=n+1;++j)
        d[i][j]=0
}

// mission impossible
// calculate all the determinants of the system
for(m=2; m<=n ; ++m) {
    for(i=m;i<=n;++i)
        for(j = m-1;j<=n+1;++j)
            d[i][j] = [_mat[i][j] * _mat[m-1][m-1] , _mat[i][m-1]]
        for(i=m;i<=n;++i)
            for(j=m-1;j<=n+1;++j) {
                _mat[i][j] = d[i][j][0]-d[i][j][1]*_mat[m-1][j] 
                if(Math.abs(_mat[i][j])<1e-25) _mat[i][j]=0  // i have to add this line
            }
}
// now the coefficients of equation (not exactly)

for(i=n;i>=1;--i) {
    c[i-1] = _mat[i][n+1]
    if (i!=n)
    for(j=n; j>=i+1;--j)
        c[i-1] = c[i-1] -_mat[i][j] * c[j-1]
    if(_mat[i][i]!=0)
        c[i-1]=c[i-1] / _mat[i][i]
    else
        c[i-1]=0
    if(Math.abs(c[i-1])<1e-25)
        c[i-1]=0
}
c.length=n
return c
}

function fitpoly(e,b){
var a=new Array()
var n = 1+b,e=[[0,0]].concat(e),ns=e.length-1
for(i=0;i<=n+1;i++) {
    a[i]=new Array();
    for(j=0;j<=n+1;++j)
        a[i][j]=0
}
for(m=1;m <= n;m++)
    for(i=1;i<= m;i++) {
        j = m - i + 1; 
        for(ii=1;ii <= ns;ii++)
            a[i][j] = a[i][j] + Math.pow(e[ii][0], m-1)
    }  
for(i=1;i<= n;++i)
    for(ii=1;ii<=ns;++ii)
        a[i][n+1] = a[i][n+1] +e[ii][1]*Math.pow(e[ii][0],i-1) 
for(m = n+2 ; m <= 2*n ; ++m)
    for(i = m-n; i<= n;++i) {
        j= m -i 
        for(ii=1; ii<=ns;++ii)
            a[i][j] = a[i][j] + Math.pow(e[ii][0],m-2) // coefficients of system
    }
a.length=a.length-1  
return _solve(a)
}


//and then
poly_degree = 6
points= [[2,2],[2,4],[4,6],[6,4],[8,2]]
// coefficients of polynome 
console.log(fitpoly(points, poly_degree))

// or solve a linear system. Here with six variables
solution = linear([[1,2,3,-2,-3,-26,52],[3,2,5,-2,4,30,-60],[6,1,-4,-1,5,94,-188],[-1,2,4,3,4,30,-60],[-1,4,2,-1,2,26,-52],[3,-3,11,-7,-2,-1,-95]])
console.log(solution)

perhaps this code might help someone

function _prepare(_mat) {
_mat=[[]].concat(_mat)
for(i=0;i<_mat.length;++i)
    _mat[i]=[0].concat(_mat[i])
return _mat
}

function linear(_mat){
_mat=_prepare(_mat)
return _solve(_mat)
}


function _solve(_mat){
var c=new Array(),d=new Array()
var n=_mat.length-1

for(i=0;i<=n+1;i++) {
    d[i]=new Array();
    c[i]=0
    for(j=0;j<=n+1;++j)
        d[i][j]=0
}

// mission impossible
// calculate all the determinants of the system
for(m=2; m<=n ; ++m) {
    for(i=m;i<=n;++i)
        for(j = m-1;j<=n+1;++j)
            d[i][j] = [_mat[i][j] * _mat[m-1][m-1] , _mat[i][m-1]]
        for(i=m;i<=n;++i)
            for(j=m-1;j<=n+1;++j) {
                _mat[i][j] = d[i][j][0]-d[i][j][1]*_mat[m-1][j] 
                if(Math.abs(_mat[i][j])<1e-25) _mat[i][j]=0  // i have to add this line
            }
}
// now the coefficients of equation (not exactly)

for(i=n;i>=1;--i) {
    c[i-1] = _mat[i][n+1]
    if (i!=n)
    for(j=n; j>=i+1;--j)
        c[i-1] = c[i-1] -_mat[i][j] * c[j-1]
    if(_mat[i][i]!=0)
        c[i-1]=c[i-1] / _mat[i][i]
    else
        c[i-1]=0
    if(Math.abs(c[i-1])<1e-25)
        c[i-1]=0
}
c.length=n
return c
}

function fitpoly(e,b){
var a=new Array()
var n = 1+b,e=[[0,0]].concat(e),ns=e.length-1
for(i=0;i<=n+1;i++) {
    a[i]=new Array();
    for(j=0;j<=n+1;++j)
        a[i][j]=0
}
for(m=1;m <= n;m++)
    for(i=1;i<= m;i++) {
        j = m - i + 1; 
        for(ii=1;ii <= ns;ii++)
            a[i][j] = a[i][j] + Math.pow(e[ii][0], m-1)
    }  
for(i=1;i<= n;++i)
    for(ii=1;ii<=ns;++ii)
        a[i][n+1] = a[i][n+1] +e[ii][1]*Math.pow(e[ii][0],i-1) 
for(m = n+2 ; m <= 2*n ; ++m)
    for(i = m-n; i<= n;++i) {
        j= m -i 
        for(ii=1; ii<=ns;++ii)
            a[i][j] = a[i][j] + Math.pow(e[ii][0],m-2) // coefficients of system
    }
a.length=a.length-1  
return _solve(a)
}


//and then
poly_degree = 6
points= [[2,2],[2,4],[4,6],[6,4],[8,2]]
// coefficients of polynome 
console.log(fitpoly(points, poly_degree))

// or solve a linear system. Here with six variables
solution = linear([[1,2,3,-2,-3,-26,52],[3,2,5,-2,4,30,-60],[6,1,-4,-1,5,94,-188],[-1,2,4,3,4,30,-60],[-1,4,2,-1,2,26,-52],[3,-3,11,-7,-2,-1,-95]])
console.log(solution)
阳光①夏 2024-12-03 00:09:53

尝试这个要点,它使用numeric.js

function polyfit(xArray, yArray, order) {

  if (xArray.length <= order) console.warn("Warning: Polyfit may be poorly conditioned.")

  let xMatrix = []
  let yMatrix = numeric.transpose([yArray])

  for (let i = 0; i < xArray.length; i++) {

    let temp = []

    for (let j = 0; j <= order; j++) {

      temp.push(Math.pow(xArray[i], j))

    }

    xMatrix.push(temp)

  }

  let xMatrixT = numeric.transpose(xMatrix)

  let dot1 = numeric.dot(xMatrixT, xMatrix)
  let dot2 = numeric.dot(xMatrixT, yMatrix)

  let dotInv = numeric.inv(dot1)

  let coefficients = numeric.dot(dotInv, dot2)

  return coefficients

}

Give this gist a try, it uses numeric.js:

function polyfit(xArray, yArray, order) {

  if (xArray.length <= order) console.warn("Warning: Polyfit may be poorly conditioned.")

  let xMatrix = []
  let yMatrix = numeric.transpose([yArray])

  for (let i = 0; i < xArray.length; i++) {

    let temp = []

    for (let j = 0; j <= order; j++) {

      temp.push(Math.pow(xArray[i], j))

    }

    xMatrix.push(temp)

  }

  let xMatrixT = numeric.transpose(xMatrix)

  let dot1 = numeric.dot(xMatrixT, xMatrix)
  let dot2 = numeric.dot(xMatrixT, yMatrix)

  let dotInv = numeric.inv(dot1)

  let coefficients = numeric.dot(dotInv, dot2)

  return coefficients

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文