N-th degree polynomial fit
Use the fitndgr function, passing the desired polynomial degree as the third argument:
using EasyFit, Plots, Random
Random.seed!(1)
x = sort(rand(10))
y = @. 1 + 2x + 3x^2 + 4x^3 + 6x^4
fit = fitndgr(x, y, 4)------------- n-th degree polynomial degree Fit -------------
Equation: y = sum(p[i] * x^(i-1) for i in n+1:-1:1)
With: p = [1.0000000000026952, 1.9999999999348628, 3.0000000003219345, 3.9999999994619757, 6.000000000289199]
Correlation coefficient, R² = 1.0
Average square residue = 6.346664549010196e-25
Predicted Y: ypred = [ 1.0499882010195154, 1.10610788901271, ...]
residues = [ 1.305178187749334e-12, 2.084998840246044e-13, ...]
-------------------------------------------------------------
The fitted coefficients p[1], p[2], ..., p[n+1] are stored in fit.lscoeff, from the independent term to the highest-degree coefficient:
fit.lscoeff5-element Vector{Float64}:
1.0000000000026952
1.9999999999348628
3.0000000003219345
3.9999999994619757
6.000000000289199scatter(x, y, label="data", framestyle=:box)
plot!(fit.x, fit.y, label="fit", linewidth=2)