I have a set of point x and y and I would like to find the best (polynomial?) fit and store it so that I can use it as a function.

Polynomials.jl creates the fit but not the function (evaluating the function at a point inside the relevant range delivers wrong results).

What’s a good package and a simple implementation?

Polynomials.jl should produce good numbers - could you post a snippet that reproduces the case where you get incorrect results?

Apologies, Polynomials works. If the order of the Polynomial is not specified and the vector is too large (about 300 points) the fit is created but the predicted values are crazy. With polynomials of smaller orders, like 5, 10, 50, the results are sensible. There is just no warning, that could be fixed.

This is Runge’s phenomenon, as well as the inherent ill-conditionedness of fitting a polynomial whose degree is close to the number of control points. If you can choose the sampling points, it’s common to use Chebyshev node spacing to mitigate the ringing effect.

If you don’t actually need to fit a particular functional form, you may just want to interpolate using a low-order piecewise polynomial via Interpolations.jl.

2 Likes

You could also fit your data with a spline, which would avoid Runge’s phenomenon.

See this previous post for an example using BSplineKit.jl.

1 Like

Radial Basis Functions are also a good choice.

You might find the `ArnoldiFit`

method in `Polynomials`

a better choice when the vector is “too large”: `fit(ArnoldiFit, x, y, ...)`

3 Likes