Engee documentation
Notebook

Construction of polynomial models

In this example, the construction of polynomials of different orders and their visualisation will be demonstrated using the Polynomials library.

Installation of the Polynomials library necessary for polynomials construction:

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

Starting the Polynomials library:

In [ ]:
using Polynomials

Forming a set of data on which to build polynomials:

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

Constructing 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

Starting a library for plotting:

In [ ]:
using Plots

Plotting the graphs of the resulting polynomials:

In [ ]:
scatter(xs,ys,label="исходные данные")
plot!(pol1, extrema(xs)..., label="полином 1-й степени")
plot!(pol5, extrema(xs)..., label="полином 5-й степени")
plot!(pol9, extrema(xs)..., label="полином 9-й степени")
plot!(pol20, extrema(xs)..., label="полином 20-й степени")
Out[0]:

Conclusion:

This example has demonstrated the construction of polynomials and their visualisation. From the graph you can estimate how much the degree of the polynomial affects its fit to the original data set.