2.7.20 List Tutorial Exercise 2

Given ‘x’ and ‘y’ vectors in quick variables 1 and 2 as before, the first job is to form the matrix that describes the problem.

   m*x + b*1 = y

Thus we want a 19x2 matrix with our ‘x’ vector as one column and ones as the other column. So, first we build the column of ones, then we combine the two columns to form our ‘A’ matrix.

2:  [1.34, 1.41, 1.49, ... ]    1:  [ [ 1.34, 1 ]
1:  [1, 1, 1, ...]                    [ 1.41, 1 ]
    .                                 [ 1.49, 1 ]
                                      …

    r 1 1 v b 19 RET                M-2 v p v t   s 3

Now we compute ‘trn(A) * y’ and ‘trn(A) * A’ and divide.

1:  [33.36554, 13.613]    2:  [33.36554, 13.613]
    .                     1:  [ [ 98.0003, 41.63 ]
                                [  41.63,   19   ] ]
                              .

 v t r 2 *                    r 3 v t r 3 *

(Hey, those numbers look familiar!)

1:  [0.52141679, -0.425978]
    .

    /

Since we were solving equations of the form ‘m*x + b*1 = y’, these numbers should be ‘m’ and ‘b’, respectively. Sure enough, they agree exactly with the result computed using V M and V R!

The moral of this story: V M and V R will probably solve your problem, but there is often an easier way using the higher-level arithmetic functions!

In fact, there is a built-in a F command that does least-squares fits. See Curve Fitting.