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.9999999999348608, 3.0000000003219673, 3.999999999461884, 6.000000000289266]
Correlation coefficient, R² = 1.0
Average square residue = 6.3494104752136575e-25
Predicted Y: ypred = [ 1.0499882010195154, 1.1061078890127098, ...]
residues = [ 1.305178187749334e-12, 2.0827783941967937e-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.9999999999348608
3.0000000003219673
3.999999999461884
6.000000000289266scatter(x, y, label="data", framestyle=:box)
plot!(fit.x, fit.y, label="fit", linewidth=2)