examples/qfit.mod
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
/*Quadratic Curve Fitting Solution
alpar@1
     2
alpar@1
     3
  Find a plausable quadratic fit to a sample of points
alpar@1
     4
alpar@1
     5
  Nigel_Galloway@operamail.com
alpar@1
     6
  February 1st., 2009
alpar@1
     7
*/
alpar@1
     8
set Sample;
alpar@1
     9
param Sx {z in Sample};
alpar@1
    10
param Sy {z in Sample};
alpar@1
    11
alpar@1
    12
var a;
alpar@1
    13
var b;
alpar@1
    14
var c;
alpar@1
    15
alpar@1
    16
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];
alpar@1
    17
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];
alpar@1
    18
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];
alpar@1
    19
alpar@1
    20
solve;
alpar@1
    21
alpar@1
    22
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);
alpar@1
    23
alpar@1
    24
data;
alpar@1
    25
alpar@1
    26
param:
alpar@1
    27
Sample:   Sx    Sy :=
alpar@1
    28
  1         0    1
alpar@1
    29
  2       0.5  0.9
alpar@1
    30
  3         1  0.7
alpar@1
    31
  4       1.5  1.5
alpar@1
    32
  5       1.9    2
alpar@1
    33
  6       2.5  2.4
alpar@1
    34
  7         3  3.2
alpar@1
    35
  8       3.5    2
alpar@1
    36
  9         4  2.7
alpar@1
    37
 10       4.5  3.5
alpar@1
    38
 11         5    1
alpar@1
    39
 12       5.5    4
alpar@1
    40
 13         6  3.6
alpar@1
    41
 14       6.6  2.7
alpar@1
    42
 15         7  5.7
alpar@1
    43
 16       7.6  4.6
alpar@1
    44
 17       8.5    6
alpar@1
    45
 18         9  6.8
alpar@1
    46
 19        10  7.3
alpar@1
    47
;
alpar@1
    48
alpar@1
    49
end;