examples/qfit.mod
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/qfit.mod	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,49 @@
     1.4 +/*Quadratic Curve Fitting Solution
     1.5 +
     1.6 +  Find a plausable quadratic fit to a sample of points
     1.7 +
     1.8 +  Nigel_Galloway@operamail.com
     1.9 +  February 1st., 2009
    1.10 +*/
    1.11 +set Sample;
    1.12 +param Sx {z in Sample};
    1.13 +param Sy {z in Sample};
    1.14 +
    1.15 +var a;
    1.16 +var b;
    1.17 +var c;
    1.18 +
    1.19 +equalz1 :sum{z in Sample} a*Sx[z]*Sx[z]*Sx[z]*Sx[z] + sum{z in Sample} b*Sx[z]*Sx[z]*Sx[z] + sum{z in Sample} c*Sx[z]*Sx[z] = sum{z in Sample} Sy[z]*Sx[z]*Sx[z];
    1.20 +equalz2 :sum{z in Sample} a*Sx[z]*Sx[z]*Sx[z] + sum{z in Sample} b*Sx[z]*Sx[z] + sum{z in Sample} c*Sx[z] = sum{z in Sample} Sy[z]*Sx[z];
    1.21 +equalz3 :sum{z in Sample} a*Sx[z]*Sx[z] + sum{z in Sample} b*Sx[z] + sum{z in Sample} c = sum{z in Sample} Sy[z];
    1.22 +
    1.23 +solve;
    1.24 +
    1.25 +printf "\nbest quadratic fit is:\n\ty = %f %s %fx %s %fx^2\n\n", c, if b < 0 then "-" else "+", abs(b), if a < 0 then "-" else "+", abs(a);
    1.26 +
    1.27 +data;
    1.28 +
    1.29 +param:
    1.30 +Sample:   Sx    Sy :=
    1.31 +  1         0    1
    1.32 +  2       0.5  0.9
    1.33 +  3         1  0.7
    1.34 +  4       1.5  1.5
    1.35 +  5       1.9    2
    1.36 +  6       2.5  2.4
    1.37 +  7         3  3.2
    1.38 +  8       3.5    2
    1.39 +  9         4  2.7
    1.40 + 10       4.5  3.5
    1.41 + 11         5    1
    1.42 + 12       5.5    4
    1.43 + 13         6  3.6
    1.44 + 14       6.6  2.7
    1.45 + 15         7  5.7
    1.46 + 16       7.6  4.6
    1.47 + 17       8.5    6
    1.48 + 18         9  6.8
    1.49 + 19        10  7.3
    1.50 +;
    1.51 +
    1.52 +end;