Tip 81. KISS Principle (by the example of least-squares method)

(Translation into English by Roger H. Faucher, M.A.T. ESL rfauch2001@yahoo.com)

Fig. 81. Universal realization of least-square method

The KISS principle is not related to kisses, although it is easy to notice its application in computer use. KISS is an acronym for the English phrase "Keep It Simple, Stupid!" It calls for using the simplest algorithm possible when solving problems. The KISS-principle is a useful one to follow when working with any program. Certainly, this tip will reduce frustration among Mathcad users.

The solution of one of the most popular problems, approximation of tabular dependence by the least-squares method in Mathcad, is shown in fig. 81. For this purpose, there are special built-in functions in Mathcad: slope, intercept, line, linfit, genfit, etc The more universal function is the last one listed genfit (general fitting), which is applicable for any type of approximation function with any number of variables. There is only one defect of genfit it's use requires a knowledge of partial derivatives of the approximation function. They can be found with help of symbolic mathematics. But thats the trouble, since the variables of the approximation function have to be written as the elements of a vector (x0, x1 and etc.). But operators and commands of symbolic mathematics are not applied for such unknowns. Separately used, the genfit function hides the calculation and makes it difficult to understand.

As the title of the tip suggests, the problem can be solved more simply (KISS principle) and more visually if we remember that it reduces to a minimization of sum of squares points departure from a curve. There is corresponding built-in function Minimize in Mathcad 8 and in more recent versions. In fig. 81, the least-squares method is "clear" and it opens for study: We find the meaning of the variables b and k where a function with the name Σ[Δ^2] (sum of squares points departure required in the approximation curve) will be a miminun.

Here are a few more tips:

  1. In fig. 81, there are no traditional commentaries as textual inset commentaries frame five semantic areas of the Mathcad document. These areas can be reduced (Collapse) and protected from editing (Lock). It helps to work with the document. For example, the Solution region occupies many pages but the Answer region lies at the bottom. If we collapse the Solution region, then the initial data (two initial vectors) and the Answer region will be near each other.
  2. Renunciation of the use of the genfit function has allowed working with a vector X, whose elements have to increase optionally (the genfit function works only with the selected vector X).
  3. Two vectors of initial data (in matrix Gr) are situated aflat with transposition but not upright. It makes the Mathcad document more compact.
  4. In fig. 81, three approximate formulas are written. We can choose the necessary formula by the exclusion of two unnecessary ones (by marking off the black rectangle in the right upper corner). Another way to exclude unnecessary functions is to put them in columns. The necessary approximation function can be switched into use if we carry it down the functions column. Mathcad allows a repeated definition of one users function but it "works" with the last one. The third way of switching a necessary function may be based on changing the style of the function name. Apart from drawing attention, it is worthwhile to tint the working smoothing function. Other dominant operators could be tinted (e.g., initial data and the answer).
  5. The Criterion function, which is minimized in fig. 81 has an unusual name Σ[Δ^2] that includes two Greek letters (Σ and 2), figure (2) and three special symbols ([, ^ and ]). This name is the abridgement of the functions full name: sum (Σ) of squares (^2) points departure a curve (Δ). To distinguish from round brackets that frame arguments of the function, square brackets are written in the functions name. Then, a function such as Σ(Δ^2)(b, k) has no competing similarities.