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,
POLYFITperforms 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.POLYVALis simply evaluating the polynomial at the points X by substituting the coefficients in the equation.