The least-squares method for approximation of data
using
Mathematica

[Graphics:HTMLFiles/index_1.gif]

Background        

Given : A table of the function  y = f (x) : { x_1, y_1}, ..., { x_n1, y_n1} .

Find : Find the algebraic polynomial of a given degree which best approximates the function, using the method of least squares (LSM) of a given degree .

RowBox[{Theoretical,  , RowBox[{overview, :,  , StyleBox[RowBox[{The,  , method,  , was,  , pr ... tFamily -> Arial, FontSize -> 18., FontWeight -> Bold, FontVariations -> {Underline -> False}]}]}]

The required polynomial  has  the form : p_k (x) = c_0 + c_1x + c_2x^2 +.. ... .        .        .         2k                                       k                           k

The mean squared error  is :     δ_k = (Underoverscript[∑, i = 1, arg3] (f (x_i) - p_k (x_i))^2)^(1/2)    .

Applications : The polynomial p (x) is used to approximate the function f (x) in the neighbourhood of some point   t  so that  p_k (t) ≈f (t) .

RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox ... 3,  , using,  , the,  , polynomial .}], FontFamily -> Arial, FontSize -> 18, FontWeight -> Bold]}]

  Solution : We define the function and draw its graph in the interval [1, 2] :

f[xx_] := xx + Sin[xx^2]      RowBox[{RowBox[{a, =, 1.}], ;, RowBox[{b, =, 2.}],  , ;}] gf = Plot[f[xx], {xx, a, b}]

[Graphics:HTMLFiles/index_11.gif]

⁃Graphics⁃

a) We calculate the values of the array of points :   x_i, y_i = f (x_i), i = 1, 2,  ... p; n1 = 11 equidistant points with a step of  h  in the interval [a, b] :

n1 = 11 ;   h = (b - a)/(n1 - 1) ; Print["Step h= ", h] x = Table[a + h *  ... stPlot, [, RowBox[{xy, ,, RowBox[{PlotStyle, , RowBox[{PointSize, [, 0.02, ]}]}]}], ]}]}]

RowBox[{Step h= , , 0.1}]

RowBox[{x=, , RowBox[{{, RowBox[{1., ,, 1.1, ,, 1.2, ,, 1.3, ,, 1.4, ,, 1.5, ,, 1.6, ,, 1.7, ,, 1.8, ,, 1.9, ,, 2.}], }}]}]

RowBox[{y=, , RowBox[{{, RowBox[{1.84147, ,, 2.03562, ,, 2.19146, ,, 2.2929, ,, 2.32521, ,, 2.27807, ,, 2.14936, ,, 1.94895, ,, 1.70175, ,, 1.44853, ,, 1.2432}], }}]}]

RowBox[{{, RowBox[{RowBox[{{, RowBox[{1., ,, 1.84147}], }}], ,, RowBox[{{, RowBox[{1.1, ,, 2.0 ... ], ,, RowBox[{{, RowBox[{1.9, ,, 1.44853}], }}], ,, RowBox[{{, RowBox[{2., ,, 1.2432}], }}]}], }}]

[Graphics:HTMLFiles/index_19.gif]

⁃Graphics⁃

b) We calculate all the sums needed for  the LSM of the first degree, we solve the c ... lve[ ] Mathematica command and extract  the coefficients of the polynomial - c0 and c1 :

s0 = n1 ; s1 = Underoverscript[∑, i = 1, arg3] x_〚i〛   ; s2 = Un ... Solve[S, d] c0 = c_〚1〛_〚1〛 c1 = c_〚2〛_〚1〛

RowBox[{{, RowBox[{RowBox[{{, RowBox[{11, ,, 16.5}], }}], ,, RowBox[{{, RowBox[{16.5, ,, 25.85}], }}]}], }}]

RowBox[{{, RowBox[{RowBox[{{, 21.4565, }}], ,, RowBox[{{, 31.4175, }}]}], }}]

RowBox[{{, RowBox[{RowBox[{{, 2.99685, }}], ,, RowBox[{{, RowBox[{-, 0.697508}], }}]}], }}]

2.99685

RowBox[{-, 0.697508}]

We define the polynomial of best approximation of the first degree of LSM as a function and dr ... raphs of the table of   f (x)   and its approximation  p1 (x) :

p1[xx_] := c0 + c1 * xx gp1 = Plot[p1[xx], {xx, a, b}, PlotStyleHue[.6]] Show[gy, gp1]

[Graphics:HTMLFiles/index_30.gif]

⁃Graphics⁃

[Graphics:HTMLFiles/index_32.gif]

⁃Graphics⁃

c) We calculate the mean squared error of the approximation :

δ_1 = (Underoverscript[∑, i = 1, arg3] (f[x_〚i〛] - p1[x_〚i〛])^2)^(1/2)

0.87215

StyleBox[RowBox[{RowBox[{d),  , The,  , approximate,  , absolute,  , value,  , of,    ... ;only on a table of measured values :}], FontFamily -> Arial, FontSize -> 18., FontWeight -> Bold]

RowBox[{StyleBox[RowBox[{p1, [, 1.33, ]}], FontColor -> GrayLevel[0.]],   }] RowBox[{Abs, [, RowBox[{RowBox[{f, [, 1.33, ]}], -, RowBox[{p1, [, 1.33, ]}]}],  , ]}]

2.06917

0.241273

Conclusion:   The obtained error δ_1 is obviously large as can also be seen from the graphs of both functions. An approximation with a polynomial of a higher degree can be found.

StyleBox[RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{Problem,  , 2.,   ... , f, RowBox[{RowBox[{(, 1.33, )}], .}]}], FontFamily -> Arial, FontSize -> 18, FontWeight -> Bold]

 Solution :

a) We calculate all the sums needed for LSM of the second degree, we solve the corresponding s ... ing LinearSolve and extract the coefficients of the polynomial, which we mark with v0, v1 and v2 :

s0 = n1 ; s1 = Underoverscript[∑, i = 1, arg3] x_〚i〛   ; s2 = Un ... 4;1〛 v1 = c_〚2〛_〚1〛 v2 = c_〚3〛_〚1〛

RowBox[{{, RowBox[{RowBox[{{, RowBox[{11, ,, 16.5, ,, 25.85}], }}], ,, RowBox[{{, RowBox[{16.5, ,, 25.85, ,, 42.075}], }}], ,, RowBox[{{, RowBox[{25.85, ,, 42.075, ,, 70.7333}], }}]}], }}]

RowBox[{{, RowBox[{RowBox[{{, 21.4565, }}], ,, RowBox[{{, 31.4175, }}], ,, RowBox[{{, 47.8688, }}]}], }}]

RowBox[{{, RowBox[{RowBox[{{, RowBox[{-, 3.32315}], }}], ,, RowBox[{{, 8.1211, }}], ,, RowBox[{{, RowBox[{-, 2.93954}], }}]}], }}]

RowBox[{-, 3.32315}]

8.1211

RowBox[{-, 2.93954}]

We define the 2nd degree polynomial providing best approximation according to the LSM, as a fu ... ltaneously display both the graphs of the table of  f (x) and its approximation p2 (x) :

p2[xx_] := v0 + v1 * xx + v2 * xx^2 gp2 = Plot[p2[xx], {xx, a, b}, PlotStyleHue[.3]] Show[gy, gp2]

[Graphics:HTMLFiles/index_54.gif]

⁃Graphics⁃

[Graphics:HTMLFiles/index_56.gif]

⁃Graphics⁃

b) We calculate the mean squared error of the approximation :

δ_2 = (Underoverscript[∑, i = 1, arg3] (f[x_〚i〛] - p2[x_〚i〛])^2)^(1/2)

0.138776

StyleBox[RowBox[{c),  , The,  , approximate,  , value,  , of,   , f, RowBox[{(, 1.33, )}],  , is :}], FontFamily -> Arial, FontSize -> 18., FontWeight -> Bold]

RowBox[{StyleBox[RowBox[{p2, [, 1.33, ]}], FontColor -> GrayLevel[0.]],   }] RowBox[{RowBox[{a ... wBox[{p2, [, 1.33, ]}]}], ]}]}], ;,  , Print[" The test absolute error now is=", aer]}]

2.27817

RowBox[{ The test absolute error now is=, , 0.0322716}]

Conclusion:   The derived errors δ_2 and aer are obviously still large but are more acceptable than the former approximation (compare the graphs of the two functions).  An approximation with a polynomial of an even higher degree can be found.

StyleBox[RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{RowBox[{Problem,  , 3.,   ... , RowBox[{RowBox[{(, 1.33, )}], .,  }]}], FontFamily -> Arial, FontSize -> 18, FontWeight -> Bold]

 Solution :

a) We calculate all the sums needed for LSM of the fifth degree, we solve the algebraic linear ... act the coefficients of the polynomial, which we mark with  z0, z1, z2 , z3, z4 and z5 :

s0 = n1 ; s1 = Underoverscript[∑, i = 1, arg3] x_〚i〛   ; s2 = Un ... 2315; ; z4 = c_〚5〛_〚1〛 ; z5 = c_〚6〛_〚1〛 ;

RowBox[{{, RowBox[{RowBox[{{, RowBox[{-, 1.28578}], }}], ,, RowBox[{{, 9.76408, }}], ,, RowBox ... owBox[{{, 22.9844, }}], ,, RowBox[{{, RowBox[{-, 12.0308}], }}], ,, RowBox[{{, 2.21355, }}]}], }}]

We define the best 5th degree approximating polynomial as a function and plot its graph . &nbs ... ltaneously display both the graphs of the table of  f (x) and its approximation p5 (x) :

p5[xx_] := z0 + z1 * xx + z2 * xx^2 + z3 * xx^3 + z4 * xx^4 + z5 * xx^5 gp5 = Plot[p5[xx], {xx, a, b}, PlotStyleHue[.8]] Show[gy, gp5]

[Graphics:HTMLFiles/index_73.gif]

⁃Graphics⁃

[Graphics:HTMLFiles/index_75.gif]

⁃Graphics⁃

b) We calculate the mean squared error of the approximation :

δ_5 = (Underoverscript[∑, i = 1, arg3] (f[x_〚i〛] - p5[x_〚i〛])^2)^(1/2)

0.00175523

StyleBox[RowBox[{c),  , The,  , approximate,  , value,  , of,   , f, RowBox[{(, 1.33, )}],  , is :}], FontFamily -> Arial, FontSize -> 18., FontWeight -> Bold]

RowBox[{StyleBox[RowBox[{p5, [, 1.33, ]}], FontColor -> GrayLevel[0.]],   }] RowBox[{RowBox[{a ... Box[{p5, [, 1.33, ]}]}], ]}]}], ;,  , Print[" The test absolute error now is=", aer5]}]

2.31092

RowBox[{ The test absolute error now is=, , 0.000477489}]

Conclusion:   The derived error δ_5 and the test absolute error are now quite satisfactory.

StyleBox[RowBox[{Problem,  , 4.,   , Display,  , the,  , solutions,  , to,  , the,   ... ms,  , on,  , the,  , same,  , graph .}], FontFamily -> Arial, FontSize -> 18, FontWeight -> Bold]

Solution :

Show[gy, gp1, gp2, gp5]

[Graphics:HTMLFiles/index_88.gif]

⁃Graphics⁃


Created by Mathematica  (May 22, 2008)