Engee documentation
Notebook

Approximation of a function by a polynomial

Introduction

In computational mathematics and experimental data processing, one of the key tasks is to restore analytical dependence on a discrete set of observations. Often, the true law describing a physical process is unknown or too complex for direct analysis, and the source data contains unavoidable measurement errors. In such cases, it becomes necessary to find a relatively simple function that best describes the available points in the sense of the minimum standard deviation. The classical least squares method solves this problem.

Importing libraries

In this example, we will need a library LinearAlgebra for operations with matrices and Printffor tabular output of results.

In [ ]:
using LinearAlgebra, Printf

Function definition

Let's define the function under study. In this example, we are examining the sinc function (cardinal sine).

In [ ]:
f(x) = x == 0 ? 1.0 : sin(x)/x
Out[0]:
f (generic function with 1 method)

Table of function values

In order to select the polynomial that best describes the behavior of the function under study, it is first necessary to set specific values of this function at specific points. Let's define the step and interval, and build a table of function values.

In [ ]:
a = 0
b = 16
step = 1.0

X = collect(a:step:b)
Y = [f(x) for x in X]

println("The table of the initial values of the function:\n")
for i in eachindex(X)
    @printf("X = %.4f, Y = %.6f\n", X[i], Y[i])
end
Table of initial function values:

X = 0.0000, Y = 1.000000
X = 1.0000, Y = 0.841471
X = 2.0000, Y = 0.454649
X = 3.0000, Y = 0.047040
X = 4.0000, Y = -0.189201
X = 5.0000, Y = -0.191785
X = 6.0000, Y = -0.046569
X = 7.0000, Y = 0.093855
X = 8.0000, Y = 0.123670
X = 9.0000, Y = 0.045791
X = 10.0000, Y = -0.054402
X = 11.0000, Y = -0.090908
X = 12.0000, Y = -0.044714
X = 13.0000, Y = 0.032321
X = 14.0000, Y = 0.070758
X = 15.0000, Y = 0.043353
X = 16.0000, Y = -0.017994

The least squares method

We implement the least squares method for selecting the coefficients of a polynomial of a given degree. To do this, it is necessary to solve a system of normal equations:

Where:

As a result, we obtain an array of coefficients of the desired polynomial, which best approximates the initial data (in the sense of the standard deviation).

In [ ]:
MNC function(x, y, degree)
    M = [x[i]^j for i in 1:length(x), j in 0:degree]
    return (M' * M) \ (M' * y)
end
Out[0]:
MNC (generic function with 1 method)

Definition of polynomials

In general, a polynomial of degree to approximate the function using the least squares method, it looks like this:

Let's define and compare polynomials of different degrees to approximate the function under study. As a result, we get a table of coefficients for the polynomials, which we will use in the future to calculate the values at new points and visually compare with the original function. This allows us to evaluate how increasing the degree of the polynomial affects the quality of the approximation.

In [ ]:
degrees = [1, 3, 5, 7, 9]
N = Dict()

println("The coefficients of the polynomials of the best RMS approximation:")

for d in degrees
    K = MNC(X, Y, d)
    N[d] = K
    
    println("\nstep = $d")
    for i in 1:length(K)
        @printf("a%d = %.6f\n", i-1, К[i])
    end
end
Coefficients of the polynomials of the best RMS approximation:

Degree = 1
a0 = 0.424370
a1 = -0.037478

Degree = 3
a0 = 1.037478
a1 = -0.406040
a2 = 0.045178
a3 = -0.001508

Degree = 5
a0 = 1.110759
a1 = -0.505507
a2 = 0.064329
a3 = -0.001316
a4 = -0.000201
a5 = 0.000009

Degree = 7
a0 = 0.983988
a1 = 0.422783
a2 = -0.716796
a3 = 0.244799
a4 = -0.037510
a5 = 0.002937
a6 = -0.000115
a7 = 0.000002

Degree = 9
a0 = 1.000660
a1 = -0.158463
a2 = 0.156316
a3 = -0.244471
a4 = 0.100740
a5 = -0.019219
a6 = 0.001989
a7 = -0.000115
a8 = 0.000004
a9 = -0.000000

Let's compare the original function with the obtained polynomials for a detailed analysis of the approximation quality. As a result, we will see how each polynomial approximates the original function over the entire interval.

In [ ]:
w = 0.1
X = collect(a:w:b)

function Value(K, x)
    sum(K[i]* x^(i-1) for i in 1:length(K))
end

println("A table of function values and polynomials:")

for x in X
    @printf("\nx = %.4f | f(x)=%.6f", x, f(x))
    
    for d in degrees
        value = Value(N[d], x)
        @printf(" | P%d=%.6f", d, значение)
    end
end
Table of function and polynomial values:

x = 0.0000 | f(x)=1.000000 | P1=0.424370 | P3=1.037478 | P5=1.110759 | P7=0.983988 | P9=1.000660
x = 0.1000 | f(x)=0.998334 | P1=0.420622 | P3=0.997324 | P5=1.060850 | P7=1.019339 | P9=0.986142
x = 0.2000 | f(x)=0.993347 | P1=0.416875 | P3=0.958065 | P5=1.012220 | P7=1.041772 | P9=0.973419
x = 0.3000 | f(x)=0.985067 | P1=0.413127 | P3=0.919691 | P5=0.964860 | P7=1.052624 | P9=0.961359
x = 0.4000 | f(x)=0.973546 | P1=0.409379 | P3=0.882194 | P5=0.918760 | P7=1.053150 | P9=0.949029
x = 0.5000 | f(x)=0.958851 | P1=0.405631 | P3=0.845564 | P5=0.873911 | P7=1.044526 | P9=0.935674
x = 0.6000 | f(x)=0.941071 | P1=0.401884 | P3=0.809792 | P5=0.830304 | P7=1.027849 | P9=0.920701
x = 0.7000 | f(x)=0.920311 | P1=0.398136 | P3=0.774870 | P5=0.787928 | P7=1.004146 | P9=0.903659
x = 0.8000 | f(x)=0.896695 | P1=0.394388 | P3=0.740788 | P5=0.746771 | P7=0.974370 | P9=0.884226
x = 0.9000 | f(x)=0.870363 | P1=0.390640 | P3=0.707537 | P5=0.706824 | P7=0.939410 | P9=0.862190
x = 1.0000 | f(x)=0.841471 | P1=0.386893 | P3=0.675108 | P5=0.668074 | P7=0.900087 | P9=0.837440
x = 1.1000 | f(x)=0.810189 | P1=0.383145 | P3=0.643492 | P5=0.630509 | P7=0.857165 | P9=0.809950
x = 1.2000 | f(x)=0.776699 | P1=0.379397 | P3=0.612681 | P5=0.594117 | P7=0.811344 | P9=0.779766
x = 1.3000 | f(x)=0.741199 | P1=0.375649 | P3=0.582664 | P5=0.558885 | P7=0.763273 | P9=0.746999
x = 1.4000 | f(x)=0.703893 | P1=0.371901 | P3=0.553433 | P5=0.524800 | P7=0.713543 | P9=0.711813
x = 1.5000 | f(x)=0.664997 | P1=0.368154 | P3=0.524980 | P5=0.491848 | P7=0.662698 | P9=0.674412
x = 1.6000 | f(x)=0.624734 | P1=0.364406 | P3=0.497294 | P5=0.460017 | P7=0.611232 | P9=0.635040
x = 1.7000 | f(x)=0.583332 | P1=0.360658 | P3=0.470367 | P5=0.429291 | P7=0.559591 | P9=0.593963
x = 1.8000 | f(x)=0.541026 | P1=0.356910 | P3=0.444189 | P5=0.399656 | P7=0.508182 | P9=0.551472
x = 1.9000 | f(x)=0.498053 | P1=0.353163 | P3=0.418753 | P5=0.371098 | P7=0.457366 | P9=0.507869
x = 2.0000 | f(x)=0.454649 | P1=0.349415 | P3=0.394048 | P5=0.343601 | P7=0.407468 | P9=0.463464
x = 2.1000 | f(x)=0.411052 | P1=0.345667 | P3=0.370066 | P5=0.317151 | P7=0.358774 | P9=0.418571
x = 2.2000 | f(x)=0.367498 | P1=0.341919 | P3=0.346797 | P5=0.291731 | P7=0.311538 | P9=0.373503
x = 2.3000 | f(x)=0.324220 | P1=0.338172 | P3=0.324233 | P5=0.267326 | P7=0.265978 | P9=0.328567
x = 2.4000 | f(x)=0.281443 | P1=0.334424 | P3=0.302364 | P5=0.243919 | P7=0.222282 | P9=0.284060
x = 2.5000 | f(x)=0.239389 | P1=0.330676 | P3=0.281182 | P5=0.221495 | P7=0.180610 | P9=0.240269
x = 2.6000 | f(x)=0.198270 | P1=0.326928 | P3=0.260678 | P5=0.200036 | P7=0.141095 | P9=0.197464
x = 2.7000 | f(x)=0.158289 | P1=0.323181 | P3=0.240841 | P5=0.179526 | P7=0.103843 | P9=0.155900
x = 2.8000 | f(x)=0.119639 | P1=0.319433 | P3=0.221664 | P5=0.159949 | P7=0.068936 | P9=0.115813
x = 2.9000 | f(x)=0.082500 | P1=0.315685 | P3=0.203137 | P5=0.141285 | P7=0.036438 | P9=0.077418
x = 3.0000 | f(x)=0.047040 | P1=0.311937 | P3=0.185252 | P5=0.123520 | P7=0.006387 | P9=0.040910
x = 3.1000 | f(x)=0.013413 | P1=0.308189 | P3=0.167999 | P5=0.106633 | P7=-0.021194 | P9=0.006460
x = 3.2000 | f(x)=-0.018242 | P1=0.304442 | P3=0.151369 | P5=0.090609 | P7=-0.046301 | P9=-0.025780
x = 3.3000 | f(x)=-0.047802 | P1=0.300694 | P3=0.135352 | P5=0.075429 | P7=-0.068945 | P9=-0.055685
x = 3.4000 | f(x)=-0.075159 | P1=0.296946 | P3=0.119941 | P5=0.061075 | P7=-0.089153 | P9=-0.083150
x = 3.5000 | f(x)=-0.100224 | P1=0.293198 | P3=0.105126 | P5=0.047528 | P7=-0.106965 | P9=-0.108094
x = 3.6000 | f(x)=-0.122922 | P1=0.289451 | P3=0.090898 | P5=0.034772 | P7=-0.122432 | P9=-0.130458
x = 3.7000 | f(x)=-0.143199 | P1=0.285703 | P3=0.077248 | P5=0.022786 | P7=-0.135617 | P9=-0.150206
x = 3.8000 | f(x)=-0.161015 | P1=0.281955 | P3=0.064167 | P5=0.011552 | P7=-0.146592 | P9=-0.167320
x = 3.9000 | f(x)=-0.176350 | P1=0.278207 | P3=0.051646 | P5=0.001053 | P7=-0.155438 | P9=-0.181808
x = 4.0000 | f(x)=-0.189201 | P1=0.274460 | P3=0.039675 | P5=-0.008732 | P7=-0.162243 | P9=-0.193691
x = 4.1000 | f(x)=-0.199580 | P1=0.270712 | P3=0.028246 | P5=-0.017820 | P7=-0.167103 | P9=-0.203013
x = 4.2000 | f(x)=-0.207518 | P1=0.266964 | P3=0.017350 | P5=-0.026231 | P7=-0.170118 | P9=-0.209833
x = 4.3000 | f(x)=-0.213062 | P1=0.263216 | P3=0.006977 | P5=-0.033983 | P7=-0.171393 | P9=-0.214227
x = 4.4000 | f(x)=-0.216273 | P1=0.259469 | P3=-0.002881 | P5=-0.041096 | P7=-0.171037 | P9=-0.216286
x = 4.5000 | f(x)=-0.217229 | P1=0.255721 | P3=-0.012233 | P5=-0.047589 | P7=-0.169163 | P9=-0.216114
x = 4.6000 | f(x)=-0.216020 | P1=0.251973 | P3=-0.021089 | P5=-0.053480 | P7=-0.165883 | P9=-0.213826
x = 4.7000 | f(x)=-0.212750 | P1=0.248225 | P3=-0.029458 | P5=-0.058788 | P7=-0.161315 | P9=-0.209551
x = 4.8000 | f(x)=-0.207534 | P1=0.244477 | P3=-0.037348 | P5=-0.063533 | P7=-0.155574 | P9=-0.203424
x = 4.9000 | f(x)=-0.200501 | P1=0.240730 | P3=-0.044768 | P5=-0.067734 | P7=-0.148778 | P9=-0.195590
x = 5.0000 | f(x)=-0.191785 | P1=0.236982 | P3=-0.051729 | P5=-0.071409 | P7=-0.141041 | P9=-0.186200
x = 5.1000 | f(x)=-0.181532 | P1=0.233234 | P3=-0.058238 | P5=-0.074578 | P7=-0.132479 | P9=-0.175409
x = 5.2000 | f(x)=-0.169895 | P1=0.229486 | P3=-0.064305 | P5=-0.077260 | P7=-0.123206 | P9=-0.163379
x = 5.3000 | f(x)=-0.157032 | P1=0.225739 | P3=-0.069938 | P5=-0.079474 | P7=-0.113333 | P9=-0.150272
x = 5.4000 | f(x)=-0.143105 | P1=0.221991 | P3=-0.075148 | P5=-0.081237 | P7=-0.102967 | P9=-0.136251
x = 5.5000 | f(x)=-0.128280 | P1=0.218243 | P3=-0.079942 | P5=-0.082570 | P7=-0.092217 | P9=-0.121482
x = 5.6000 | f(x)=-0.112726 | P1=0.214495 | P3=-0.084330 | P5=-0.083491 | P7=-0.081183 | P9=-0.106127
x = 5.7000 | f(x)=-0.096611 | P1=0.210748 | P3=-0.088322 | P5=-0.084018 | P7=-0.069964 | P9=-0.090348
x = 5.8000 | f(x)=-0.080104 | P1=0.207000 | P3=-0.091925 | P5=-0.084170 | P7=-0.058656 | P9=-0.074301
x = 5.9000 | f(x)=-0.063369 | P1=0.203252 | P3=-0.095150 | P5=-0.083966 | P7=-0.047348 | P9=-0.058140
x = 6.0000 | f(x)=-0.046569 | P1=0.199504 | P3=-0.098004 | P5=-0.083423 | P7=-0.036127 | P9=-0.042014
x = 6.1000 | f(x)=-0.029863 | P1=0.195757 | P3=-0.100498 | P5=-0.082560 | P7=-0.025074 | P9=-0.026064
x = 6.2000 | f(x)=-0.013402 | P1=0.192009 | P3=-0.102640 | P5=-0.081394 | P7=-0.014264 | P9=-0.010425
x = 6.3000 | f(x)=0.002669 | P1=0.188261 | P3=-0.104439 | P5=-0.079945 | P7=-0.003770 | P9=0.004775
x = 6.4000 | f(x)=0.018211 | P1=0.184513 | P3=-0.105905 | P5=-0.078228 | P7=0.006344 | P9=0.019417
x = 6.5000 | f(x)=0.033095 | P1=0.180765 | P3=-0.107046 | P5=-0.076262 | P7=0.016017 | P9=0.033391
x = 6.6000 | f(x)=0.047203 | P1=0.177018 | P3=-0.107871 | P5=-0.074065 | P7=0.025194 | P9=0.046595
x = 6.7000 | f(x)=0.060425 | P1=0.173270 | P3=-0.108390 | P5=-0.071652 | P7=0.033826 | P9=0.058938
x = 6.8000 | f(x)=0.072664 | P1=0.169522 | P3=-0.108611 | P5=-0.069042 | P7=0.041868 | P9=0.070339
x = 6.9000 | f(x)=0.083832 | P1=0.165774 | P3=-0.108544 | P5=-0.066251 | P7=0.049283 | P9=0.080728
x = 7.0000 | f(x)=0.093855 | P1=0.162027 | P3=-0.108197 | P5=-0.063295 | P7=0.056037 | P9=0.090043
x = 7.1000 | f(x)=0.102672 | P1=0.158279 | P3=-0.107580 | P5=-0.060190 | P7=0.062103 | P9=0.098235
x = 7.2000 | f(x)=0.110232 | P1=0.154531 | P3=-0.106702 | P5=-0.056953 | P7=0.067460 | P9=0.105266
x = 7.3000 | f(x)=0.116498 | P1=0.150783 | P3=-0.105571 | P5=-0.053600 | P7=0.072091 | P9=0.111106
x = 7.4000 | f(x)=0.121447 | P1=0.147036 | P3=-0.104197 | P5=-0.050145 | P7=0.075984 | P9=0.115740
x = 7.5000 | f(x)=0.125067 | P1=0.143288 | P3=-0.102589 | P5=-0.046604 | P7=0.079134 | P9=0.119158
x = 7.6000 | f(x)=0.127358 | P1=0.139540 | P3=-0.100756 | P5=-0.042991 | P7=0.081539 | P9=0.121364
x = 7.7000 | f(x)=0.128334 | P1=0.135792 | P3=-0.098707 | P5=-0.039323 | P7=0.083203 | P9=0.122371
x = 7.8000 | f(x)=0.128018 | P1=0.132045 | P3=-0.096450 | P5=-0.035612 | P7=0.084135 | P9=0.122202
x = 7.9000 | f(x)=0.126448 | P1=0.128297 | P3=-0.093996 | P5=-0.031872 | P7=0.084347 | P9=0.120887
x = 8.0000 | f(x)=0.123670 | P1=0.124549 | P3=-0.091353 | P5=-0.028118 | P7=0.083857 | P9=0.118467
x = 8.1000 | f(x)=0.119739 | P1=0.120801 | P3=-0.088530 | P5=-0.024363 | P7=0.082687 | P9=0.114990
x = 8.2000 | f(x)=0.114723 | P1=0.117053 | P3=-0.085535 | P5=-0.020619 | P7=0.080861 | P9=0.110512
x = 8.3000 | f(x)=0.108695 | P1=0.113306 | P3=-0.082380 | P5=-0.016900 | P7=0.078408 | P9=0.105096
x = 8.4000 | f(x)=0.101738 | P1=0.109558 | P3=-0.079071 | P5=-0.013216 | P7=0.075361 | P9=0.098812
x = 8.5000 | f(x)=0.093940 | P1=0.105810 | P3=-0.075618 | P5=-0.009581 | P7=0.071756 | P9=0.091734
x = 8.6000 | f(x)=0.085395 | P1=0.102062 | P3=-0.072031 | P5=-0.006006 | P7=0.067631 | P9=0.083943
x = 8.7000 | f(x)=0.076203 | P1=0.098315 | P3=-0.068318 | P5=-0.002501 | P7=0.063027 | P9=0.075524
x = 8.8000 | f(x)=0.066468 | P1=0.094567 | P3=-0.064489 | P5=0.000922 | P7=0.057987 | P9=0.066564
x = 8.9000 | f(x)=0.056294 | P1=0.090819 | P3=-0.060552 | P5=0.004254 | P7=0.052557 | P9=0.057154
x = 9.0000 | f(x)=0.045791 | P1=0.087071 | P3=-0.056517 | P5=0.007485 | P7=0.046785 | P9=0.047389
x = 9.1000 | f(x)=0.035066 | P1=0.083324 | P3=-0.052392 | P5=0.010605 | P7=0.040720 | P9=0.037361
x = 9.2000 | f(x)=0.024227 | P1=0.079576 | P3=-0.048186 | P5=0.013605 | P7=0.034411 | P9=0.027168
x = 9.3000 | f(x)=0.013382 | P1=0.075828 | P3=-0.043910 | P5=0.016478 | P7=0.027910 | P9=0.016903
x = 9.4000 | f(x)=0.002636 | P1=0.072080 | P3=-0.039571 | P5=0.019215 | P7=0.021269 | P9=0.006660
x = 9.5000 | f(x)=-0.007911 | P1=0.068333 | P3=-0.035178 | P5=0.021809 | P7=0.014539 | P9=-0.003466
x = 9.6000 | f(x)=-0.018159 | P1=0.064585 | P3=-0.030742 | P5=0.024253 | P7=0.007773 | P9=-0.013387
x = 9.7000 | f(x)=-0.028017 | P1=0.060837 | P3=-0.026270 | P5=0.026540 | P7=0.001023 | P9=-0.023014
x = 9.8000 | f(x)=-0.037396 | P1=0.057089 | P3=-0.021772 | P5=0.028665 | P7=-0.005659 | P9=-0.032265
x = 9.9000 | f(x)=-0.046216 | P1=0.053341 | P3=-0.017257 | P5=0.030622 | P7=-0.012225 | P9=-0.041059
x = 10.0000 | f(x)=-0.054402 | P1=0.049594 | P3=-0.012734 | P5=0.032406 | P7=-0.018622 | P9=-0.049322
x = 10.1000 | f(x)=-0.061888 | P1=0.045846 | P3=-0.008212 | P5=0.034013 | P7=-0.024805 | P9=-0.056986
x = 10.2000 | f(x)=-0.068615 | P1=0.042098 | P3=-0.003700 | P5=0.035440 | P7=-0.030725 | P9=-0.063987
x = 10.3000 | f(x)=-0.074533 | P1=0.038350 | P3=0.000793 | P5=0.036682 | P7=-0.036340 | P9=-0.070268
x = 10.4000 | f(x)=-0.079599 | P1=0.034603 | P3=0.005258 | P5=0.037738 | P7=-0.041605 | P9=-0.075780
x = 10.5000 | f(x)=-0.083781 | P1=0.030855 | P3=0.009685 | P5=0.038606 | P7=-0.046482 | P9=-0.080480
x = 10.6000 | f(x)=-0.087054 | P1=0.027107 | P3=0.014067 | P5=0.039284 | P7=-0.050933 | P9=-0.084334
x = 10.7000 | f(x)=-0.089405 | P1=0.023359 | P3=0.018393 | P5=0.039771 | P7=-0.054924 | P9=-0.087314
x = 10.8000 | f(x)=-0.090827 | P1=0.019612 | P3=0.022655 | P5=0.040068 | P7=-0.058423 | P9=-0.089402
x = 10.9000 | f(x)=-0.091324 | P1=0.015864 | P3=0.026843 | P5=0.040174 | P7=-0.061403 | P9=-0.090587
x = 11.0000 | f(x)=-0.090908 | P1=0.012116 | P3=0.030949 | P5=0.040091 | P7=-0.063840 | P9=-0.090866
x = 11.1000 | f(x)=-0.089599 | P1=0.008368 | P3=0.034964 | P5=0.039821 | P7=-0.065712 | P9=-0.090245
x = 11.2000 | f(x)=-0.087427 | P1=0.004621 | P3=0.038878 | P5=0.039365 | P7=-0.067003 | P9=-0.088738
x = 11.3000 | f(x)=-0.084426 | P1=0.000873 | P3=0.042683 | P5=0.038728 | P7=-0.067700 | P9=-0.086367
x = 11.4000 | f(x)=-0.080643 | P1=-0.002875 | P3=0.046369 | P5=0.037913 | P7=-0.067795 | P9=-0.083163
x = 11.5000 | f(x)=-0.076126 | P1=-0.006623 | P3=0.049927 | P5=0.036924 | P7=-0.067284 | P9=-0.079164
x = 11.6000 | f(x)=-0.070933 | P1=-0.010371 | P3=0.053349 | P5=0.035767 | P7=-0.066166 | P9=-0.074415
x = 11.7000 | f(x)=-0.065127 | P1=-0.014118 | P3=0.056625 | P5=0.034447 | P7=-0.064446 | P9=-0.068968
x = 11.8000 | f(x)=-0.058773 | P1=-0.017866 | P3=0.059746 | P5=0.032971 | P7=-0.062133 | P9=-0.062882
x = 11.9000 | f(x)=-0.051944 | P1=-0.021614 | P3=0.062704 | P5=0.031347 | P7=-0.059241 | P9=-0.056223
x = 12.0000 | f(x)=-0.044714 | P1=-0.025362 | P3=0.065489 | P5=0.029583 | P7=-0.055788 | P9=-0.049061
x = 12.1000 | f(x)=-0.037161 | P1=-0.029109 | P3=0.068091 | P5=0.027687 | P7=-0.051797 | P9=-0.041471
x = 12.2000 | f(x)=-0.029363 | P1=-0.032857 | P3=0.070503 | P5=0.025669 | P7=-0.047295 | P9=-0.033534
x = 12.3000 | f(x)=-0.021401 | P1=-0.036605 | P3=0.072715 | P5=0.023540 | P7=-0.042314 | P9=-0.025332
x = 12.4000 | f(x)=-0.013355 | P1=-0.040353 | P3=0.074718 | P5=0.021311 | P7=-0.036891 | P9=-0.016951
x = 12.5000 | f(x)=-0.005306 | P1=-0.044100 | P3=0.076502 | P5=0.018994 | P7=-0.031065 | P9=-0.008480
x = 12.6000 | f(x)=0.002668 | P1=-0.047848 | P3=0.078060 | P5=0.016601 | P7=-0.024883 | P9=-0.000007
x = 12.7000 | f(x)=0.010491 | P1=-0.051596 | P3=0.079381 | P5=0.014147 | P7=-0.018392 | P9=0.008379
x = 12.8000 | f(x)=0.018087 | P1=-0.055344 | P3=0.080457 | P5=0.011645 | P7=-0.011645 | P9=0.016589
x = 12.9000 | f(x)=0.025386 | P1=-0.059091 | P3=0.081279 | P5=0.009112 | P7=-0.004698 | P9=0.024537
x = 13.0000 | f(x)=0.032321 | P1=-0.062839 | P3=0.081838 | P5=0.006562 | P7=0.002388 | P9=0.032139
x = 13.1000 | f(x)=0.038829 | P1=-0.066587 | P3=0.082124 | P5=0.004014 | P7=0.009551 | P9=0.039314
x = 13.2000 | f(x)=0.044854 | P1=-0.070335 | P3=0.082129 | P5=0.001486 | P7=0.016727 | P9=0.045987
x = 13.3000 | f(x)=0.050344 | P1=-0.074083 | P3=0.081843 | P5=-0.001005 | P7=0.023846 | P9=0.052087
x = 13.4000 | f(x)=0.055252 | P1=-0.077830 | P3=0.081258 | P5=-0.003438 | P7=0.030840 | P9=0.057550
x = 13.5000 | f(x)=0.059540 | P1=-0.081578 | P3=0.080365 | P5=-0.005793 | P7=0.037635 | P9=0.062319
x = 13.6000 | f(x)=0.063174 | P1=-0.085326 | P3=0.079153 | P5=-0.008048 | P7=0.044161 | P9=0.066345
x = 13.7000 | f(x)=0.066128 | P1=-0.089074 | P3=0.077616 | P5=-0.010181 | P7=0.050343 | P9=0.069589
x = 13.8000 | f(x)=0.068384 | P1=-0.092821 | P3=0.075742 | P5=-0.012169 | P7=0.056107 | P9=0.072020
x = 13.9000 | f(x)=0.069929 | P1=-0.096569 | P3=0.073524 | P5=-0.013987 | P7=0.061381 | P9=0.073617
x = 14.0000 | f(x)=0.070758 | P1=-0.100317 | P3=0.070952 | P5=-0.015609 | P7=0.066093 | P9=0.074371
x = 14.1000 | f(x)=0.070873 | P1=-0.104065 | P3=0.068017 | P5=-0.017011 | P7=0.070171 | P9=0.074282
x = 14.2000 | f(x)=0.070284 | P1=-0.107812 | P3=0.064710 | P5=-0.018164 | P7=0.073548 | P9=0.073363
x = 14.3000 | f(x)=0.069005 | P1=-0.111560 | P3=0.061023 | P5=-0.019040 | P7=0.076159 | P9=0.071637
x = 14.4000 | f(x)=0.067060 | P1=-0.115308 | P3=0.056945 | P5=-0.019611 | P7=0.077942 | P9=0.069140
x = 14.5000 | f(x)=0.064476 | P1=-0.119056 | P3=0.052469 | P5=-0.019845 | P7=0.078841 | P9=0.065916
x = 14.6000 | f(x)=0.061287 | P1=-0.122803 | P3=0.047585 | P5=-0.019713 | P7=0.078805 | P9=0.062023
x = 14.7000 | f(x)=0.057534 | P1=-0.126551 | P3=0.042283 | P5=-0.019181 | P7=0.077790 | P9=0.057526
x = 14.8000 | f(x)=0.053260 | P1=-0.130299 | P3=0.036555 | P5=-0.018216 | P7=0.075757 | P9=0.052500
x = 14.9000 | f(x)=0.048516 | P1=-0.134047 | P3=0.030392 | P5=-0.016784 | P7=0.072679 | P9=0.047028
x = 15.0000 | f(x)=0.043353 | P1=-0.137795 | P3=0.023785 | P5=-0.014850 | P7=0.068535 | P9=0.041199
x = 15.1000 | f(x)=0.037828 | P1=-0.141542 | P3=0.016725 | P5=-0.012376 | P7=0.063318 | P9=0.035105
x = 15.2000 | f(x)=0.032000 | P1=-0.145290 | P3=0.009202 | P5=-0.009327 | P7=0.057029 | P9=0.028842
x = 15.3000 | f(x)=0.025931 | P1=-0.149038 | P3=0.001208 | P5=-0.005662 | P7=0.049686 | P9=0.022506
x = 15.4000 | f(x)=0.019683 | P1=-0.152786 | P3=-0.007266 | P5=-0.001343 | P7=0.041317 | P9=0.016186
x = 15.5000 | f(x)=0.013320 | P1=-0.156533 | P3=-0.016230 | P5=0.003672 | P7=0.031968 | P9=0.009969
x = 15.6000 | f(x)=0.006907 | P1=-0.160281 | P3=-0.025692 | P5=0.009424 | P7=0.021701 | P9=0.003929
x = 15.7000 | f(x)=0.000507 | P1=-0.164029 | P3=-0.035662 | P5=0.015957 | P7=0.010597 | P9=-0.001875
x = 15.8000 | f(x)=-0.005817 | P1=-0.167777 | P3=-0.046149 | P5=0.023315 | P7=-0.001245 | P9=-0.007401
x = 15.9000 | f(x)=-0.012004 | P1=-0.171524 | P3=-0.057161 | P5=0.031544 | P7=-0.013702 | P9=-0.012636
x = 16.0000 | f(x)=-0.017994 | P1=-0.175272 | P3=-0.068707 | P5=0.040689 | P7=-0.026631 | P9=-0.017594

Approximation by a 1st-order polynomial

Let's construct graphs for visual comparison of the function and its approximation by a first-order polynomial.

In [ ]:
d = 1
график = plot(X, [f(x) for x in X], label="f(x)=sin(x)/x", lw=2)
plot!(X, [Значение(П[d], x) for x in X], label="The $d-th order polynomial", 
xlabel="X", ylabel="Y", lw=2, legend=:right)
display(graph)
No description has been provided for this image

A first-order polynomial is a linear function. Obviously, there is no overlap with the function under study.

Approximation by a 3rd-order polynomial

Let's construct graphs for visual comparison of the function and its approximation by a third-order polynomial.

In [ ]:
d = 3
график = plot(X, [f(x) for x in X], label="f(x)=sin(x)/x", lw=2)
plot!(X, [Значение(П[d], x) for x in X], label="The $d-th order polynomial", 
xlabel="X", ylabel="Y", lw=2, legend=:right)
display(graph)
No description has been provided for this image

A 3rd-order polynomial is a cubic function, and the match is getting closer.

Approximation by a 5th order polynomial

Let's construct graphs for visual comparison of the function and its approximation by a fifth-order polynomial.

In [ ]:
d = 5
график = plot(X, [f(x) for x in X], label="f(x)=sin(x)/x", lw=2)
plot!(X, [Значение(П[d], x) for x in X], label="The $d-th order polynomial", 
xlabel="X", ylabel="Y", lw=2, legend=:right)
display(graph)
No description has been provided for this image

When approximated by a 5th order polynomial, the match gets even closer.

Approximation by a 7th-order polynomial

Let's build graphs for visual comparison of the function and its approximation by a seventh-order polynomial.

In [ ]:
d = 7
график = plot(X, [f(x) for x in X], label="f(x)=sin(x)/x", lw=2)
plot!(X, [Значение(П[d], x) for x in X], label="The $d-th order polynomial", 
xlabel="X", ylabel="Y", lw=2, legend=:right)
display(graph)
No description has been provided for this image

When approximated by a 7th-order polynomial, the match becomes even closer than when approximated by a 5th-order polynomial.

Approximation by a 9th-order polynomial

Let's build graphs for visual comparison of the function and its approximation by a ninth-order polynomial.

In [ ]:
d = 9
график = plot(X, [f(x) for x in X], label="f(x)=sin(x)/x", lw=2)
plot!(X, [Значение(П[d], x) for x in X], label="The $d-th order polynomial", 
xlabel="X", ylabel="Y", lw=2, legend=:right)
display(graph)
No description has been provided for this image

When approximated by a 9th-order polynomial, the function under study practically coincides with the polynomial. Thus, it can be concluded that the higher the order of the polynomial, the higher its coincidence with the function under study.

Conclusion

In this example, the classical least squares polynomial approximation problem was numerically implemented and investigated, which is a fundamental tool in a wide variety of fields of activity where it is necessary to restore dependencies on noisy or discrete data.:

  • Physics and engineering — processing of experimental results (for example, approximation of volt-ampere characteristics, calibration of sensors, smoothing of signals).

  • Economics and Finance — building time series trends, forecasting prices, and analyzing market indicators.

  • Biology and medicine — modeling of dose dependencies, population growth analysis, processing of clinical trial data.

  • Geophysics and astronomy — extraction of a useful signal against a background of noise, interpolation of satellite data.

  • Machine learning — data generalization (linear and polynomial regression) as the basis of many more complex algorithms.

Thus, the approach considered in the example is not limited to a purely educational task, but forms the basis for a variety of applied numerical methods. Choosing Engee allows you to further scale calculations to large amounts of data without loss of performance, which is especially important when solving real engineering and scientific problems.