author | Alpar Juttner <alpar@cs.elte.hu> |
Mon, 06 Dec 2010 13:09:21 +0100 | |
changeset 1 | c445c931472f |
permissions | -rw-r--r-- |
alpar@1 | 1 |
/* |
alpar@1 | 2 |
|
alpar@1 | 3 |
Curve fitting problem 12.11(a) H P Williams "Model Building in Mathematical Programming" |
alpar@1 | 4 |
|
alpar@1 | 5 |
Dr. H J Mackenzie |
alpar@1 | 6 |
HARD software |
alpar@1 | 7 |
hjm@hardsoftware.com |
alpar@1 | 8 |
|
alpar@1 | 9 |
2006-01-05 |
alpar@1 | 10 |
|
alpar@1 | 11 |
*/ |
alpar@1 | 12 |
|
alpar@1 | 13 |
# set of points |
alpar@1 | 14 |
|
alpar@1 | 15 |
set I; |
alpar@1 | 16 |
|
alpar@1 | 17 |
# independent variable |
alpar@1 | 18 |
|
alpar@1 | 19 |
param x {i in I}; |
alpar@1 | 20 |
|
alpar@1 | 21 |
# dependent variable |
alpar@1 | 22 |
|
alpar@1 | 23 |
param y {i in I}; |
alpar@1 | 24 |
|
alpar@1 | 25 |
# output input values |
alpar@1 | 26 |
|
alpar@1 | 27 |
printf {i in I} "x = %.1f; y = %.1f\n", x[i], y[i]; |
alpar@1 | 28 |
|
alpar@1 | 29 |
# define equation variables |
alpar@1 | 30 |
|
alpar@1 | 31 |
var a; |
alpar@1 | 32 |
|
alpar@1 | 33 |
var b; |
alpar@1 | 34 |
|
alpar@1 | 35 |
var u {i in I}, >= 0; |
alpar@1 | 36 |
|
alpar@1 | 37 |
var v {i in I}, >= 0; |
alpar@1 | 38 |
|
alpar@1 | 39 |
# define objective function |
alpar@1 | 40 |
|
alpar@1 | 41 |
minimize error: sum {i in I} u[i] + sum {i in I} v[i]; |
alpar@1 | 42 |
|
alpar@1 | 43 |
# define equation constraint |
alpar@1 | 44 |
|
alpar@1 | 45 |
s.t. equation {i in I} : b * x[i] + a + u[i] - v[i] = y[i]; |
alpar@1 | 46 |
|
alpar@1 | 47 |
solve; |
alpar@1 | 48 |
|
alpar@1 | 49 |
printf "y = %.4fx + %.4f\n", b, a; |
alpar@1 | 50 |
|
alpar@1 | 51 |
/* |
alpar@1 | 52 |
* |
alpar@1 | 53 |
* DATA section |
alpar@1 | 54 |
* |
alpar@1 | 55 |
*/ |
alpar@1 | 56 |
|
alpar@1 | 57 |
data; |
alpar@1 | 58 |
|
alpar@1 | 59 |
param : I : x y := |
alpar@1 | 60 |
1 0 1 |
alpar@1 | 61 |
2 0.5 0.9 |
alpar@1 | 62 |
3 1 0.7 |
alpar@1 | 63 |
4 1.5 1.5 |
alpar@1 | 64 |
5 1.9 2 |
alpar@1 | 65 |
6 2.5 2.4 |
alpar@1 | 66 |
7 3 3.2 |
alpar@1 | 67 |
8 3.5 2 |
alpar@1 | 68 |
9 4 2.7 |
alpar@1 | 69 |
10 4.5 3.5 |
alpar@1 | 70 |
11 5 1 |
alpar@1 | 71 |
12 5.5 4 |
alpar@1 | 72 |
13 6 3.6 |
alpar@1 | 73 |
14 6.6 2.7 |
alpar@1 | 74 |
15 7 5.7 |
alpar@1 | 75 |
16 7.6 4.6 |
alpar@1 | 76 |
17 8.5 6 |
alpar@1 | 77 |
18 9 6.8 |
alpar@1 | 78 |
19 10 7.3 |
alpar@1 | 79 |
; |
alpar@1 | 80 |
|
alpar@1 | 81 |
end; |