Engee documentation
Notebook

Building polynomial models

In this example, using the specialized library Polynomials, the construction of polynomials of various orders and their visualization will be demonstrated.

Installing the library of Polynomials required for building polynomials:

In [ ]:
Pkg.add("Polynomials")

Launching the Polynomials Library:

In [ ]:
using Polynomials

Forming a set of data from which the polynomials will be constructed:

In [ ]:
xs = collect(0:20); ys = @. exp(-xs) + sin(xs);

Building polynomial models:

In [ ]:
pol1 = fit(xs,ys,1)
pol5 = fit(xs,ys,5)
pol9 = fit(xs,ys,9)
pol20 = fit(xs,ys,20)
println(pol1)
0.411552 - 0.0288685*x

Launching the charting library:

In [ ]:
using Plots

Plotting the resulting polynomials:

In [ ]:
scatter(xs,ys,label="initial data")
plot!(pol1, extrema(xs)..., label="a polynomial of the 1st degree")
plot!(pol5, extrema(xs)..., label="a 5th degree polynomial")
plot!(pol9, extrema(xs)..., label="a polynomial of the 9th degree")
plot!(pol20, extrema(xs)..., label="20th degree polynomial")
Out[0]:

Conclusion:

In this example, the construction of polynomials and their visualization were demonstrated. Using the graph, you can estimate how much the degree of the polynomial affects its fit to the original data set.