examples/cflsq.mod
changeset 1 c445c931472f
equal deleted inserted replaced
-1:000000000000 0:bfa35fc1c507
       
     1 /*Curve fitting problem by Least Squares
       
     2   Nigel_Galloway@operamail.com
       
     3   October 1st., 2007
       
     4 */
       
     5 set Sample;
       
     6 param Sx {z in Sample};
       
     7 param Sy {z in Sample};
       
     8 
       
     9 var X;
       
    10 var Y;
       
    11 var Ex{z in Sample};
       
    12 var Ey{z in Sample};
       
    13 
       
    14 /* sum of variances is zero for Sx*/
       
    15 variencesX{z in Sample}: X + Ex[z] = Sx[z];
       
    16 zumVariancesX: sum{z in Sample} Ex[z] = 0;
       
    17 /* sum of variances is zero for Sy*/
       
    18 variencesY{z in Sample}: Y + Ey[z] = Sy[z];
       
    19 zumVariancesY: sum{z in Sample} Ey[z] = 0;
       
    20 
       
    21 solve;
       
    22 
       
    23 param b1 := (sum{z in Sample} Ex[z]*Ey[z])/(sum{z in Sample} Ex[z]*Ex[z]);
       
    24 printf "\nbest linear fit is:\n\ty = %f %s %fx\n\n", Y-b1*X, if b1 < 0 then "-" else "+", abs(b1);
       
    25 
       
    26 data;
       
    27 
       
    28 param:
       
    29 Sample:   Sx    Sy :=
       
    30   1         0    1
       
    31   2       0.5  0.9
       
    32   3         1  0.7
       
    33   4       1.5  1.5
       
    34   5       1.9    2
       
    35   6       2.5  2.4
       
    36   7         3  3.2
       
    37   8       3.5    2
       
    38   9         4  2.7
       
    39  10       4.5  3.5
       
    40  11         5    1
       
    41  12       5.5    4
       
    42  13         6  3.6
       
    43  14       6.6  2.7
       
    44  15         7  5.7
       
    45  16       7.6  4.6
       
    46  17       8.5    6
       
    47  18         9  6.8
       
    48  19        10  7.3
       
    49 ;
       
    50 
       
    51 end;