# Package for fitting a curve and storing the function

**URL:** <https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135>\
**Category:** General Usage\
**Created:** [August 10, 2021, 2:05pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135 "2021-08-10T14:05:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)\
**Post date:** [August 10, 2021, 2:05pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/1 "2021-08-10T14:05:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 10, 2021, 2:12pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/2 "2021-08-10T14:12:43Z")

</div>

LsqFit.jl?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 10, 2021, 2:17pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/3 "2021-08-10T14:17:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)\
**Post date:** [August 10, 2021, 2:32pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/4 "2021-08-10T14:32:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [August 10, 2021, 2:54pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/5 "2021-08-10T14:54:34Z")

</div>

This is [Runge’s phenomenon](https://en.wikipedia.org/wiki/Runge%27s_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](https://en.wikipedia.org/wiki/Chebyshev_nodes) 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](http://juliamath.github.io/Interpolations.jl/latest/control/).

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [August 10, 2021, 3:17pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/6 "2021-08-10T15:17:28Z")

</div>

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

See [this previous post](https://discourse.julialang.org/t/smoothing-interpolation-for-tachometer-data/56117/11) for an example using [BSplineKit.jl](https://github.com/jipolanco/BSplineKit.jl).

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 10, 2021, 3:30pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/7 "2021-08-10T15:30:32Z")

</div>

Radial Basis Functions are also a good choice.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [August 12, 2021, 2:08pm UTC](https://discourse.julialang.org/t/package-for-fitting-a-curve-and-storing-the-function/66135/8 "2021-08-12T14:08:53Z")

</div>

You might find the `ArnoldiFit` method in `Polynomials` a better choice when the vector is “too large”: `fit(ArnoldiFit, x, y, ...)`
