examples/cflsq.mod
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/cflsq.mod	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,51 @@
     1.4 +/*Curve fitting problem by Least Squares
     1.5 +  Nigel_Galloway@operamail.com
     1.6 +  October 1st., 2007
     1.7 +*/
     1.8 +set Sample;
     1.9 +param Sx {z in Sample};
    1.10 +param Sy {z in Sample};
    1.11 +
    1.12 +var X;
    1.13 +var Y;
    1.14 +var Ex{z in Sample};
    1.15 +var Ey{z in Sample};
    1.16 +
    1.17 +/* sum of variances is zero for Sx*/
    1.18 +variencesX{z in Sample}: X + Ex[z] = Sx[z];
    1.19 +zumVariancesX: sum{z in Sample} Ex[z] = 0;
    1.20 +/* sum of variances is zero for Sy*/
    1.21 +variencesY{z in Sample}: Y + Ey[z] = Sy[z];
    1.22 +zumVariancesY: sum{z in Sample} Ey[z] = 0;
    1.23 +
    1.24 +solve;
    1.25 +
    1.26 +param b1 := (sum{z in Sample} Ex[z]*Ey[z])/(sum{z in Sample} Ex[z]*Ex[z]);
    1.27 +printf "\nbest linear fit is:\n\ty = %f %s %fx\n\n", Y-b1*X, if b1 < 0 then "-" else "+", abs(b1);
    1.28 +
    1.29 +data;
    1.30 +
    1.31 +param:
    1.32 +Sample:   Sx    Sy :=
    1.33 +  1         0    1
    1.34 +  2       0.5  0.9
    1.35 +  3         1  0.7
    1.36 +  4       1.5  1.5
    1.37 +  5       1.9    2
    1.38 +  6       2.5  2.4
    1.39 +  7         3  3.2
    1.40 +  8       3.5    2
    1.41 +  9         4  2.7
    1.42 + 10       4.5  3.5
    1.43 + 11         5    1
    1.44 + 12       5.5    4
    1.45 + 13         6  3.6
    1.46 + 14       6.6  2.7
    1.47 + 15         7  5.7
    1.48 + 16       7.6  4.6
    1.49 + 17       8.5    6
    1.50 + 18         9  6.8
    1.51 + 19        10  7.3
    1.52 +;
    1.53 +
    1.54 +end;