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