alpar@9: /* alpar@9: alpar@9: Curve fitting problem 12.11(b) H P Williams "Model Building in Mathematical Programming" alpar@9: alpar@9: Dr. H J Mackenzie alpar@9: HARD software alpar@9: hjm@hardsoftware.com alpar@9: alpar@9: 2006-01-23 alpar@9: alpar@9: */ alpar@9: alpar@9: # set of points alpar@9: alpar@9: set I; alpar@9: alpar@9: # independent variable alpar@9: alpar@9: param x {i in I}; alpar@9: alpar@9: # dependent variable alpar@9: alpar@9: param y {i in I}; alpar@9: alpar@9: # output input values alpar@9: alpar@9: printf {i in I} "x = %.1f; y = %.1f\n", x[i], y[i]; alpar@9: alpar@9: # define equation variables alpar@9: alpar@9: var a; alpar@9: alpar@9: var b; alpar@9: alpar@9: var u {i in I}, >= 0; alpar@9: alpar@9: var v {i in I}, >= 0; alpar@9: alpar@9: var z; alpar@9: alpar@9: # define objective function alpar@9: alpar@9: minimize deviation: z; alpar@9: alpar@9: # define equation constraint alpar@9: alpar@9: s.t. equation {i in I} : b * x[i] + a + u[i] - v[i] = y[i]; alpar@9: alpar@9: # define deviation constrains alpar@9: alpar@9: s.t. u_deviation {i in I} : z - u[i] >= 0; alpar@9: s.t. v_deviation {i in I} : z - v[i] >= 0; alpar@9: alpar@9: solve; alpar@9: alpar@9: printf "y = %.4fx + %.4f Max deviation = %.4f\n", b, a, z; alpar@9: alpar@9: /* alpar@9: * alpar@9: * DATA section alpar@9: * alpar@9: */ alpar@9: alpar@9: data; alpar@9: alpar@9: param : I : x y := alpar@9: 1 0 1 alpar@9: 2 0.5 0.9 alpar@9: 3 1 0.7 alpar@9: 4 1.5 1.5 alpar@9: 5 1.9 2 alpar@9: 6 2.5 2.4 alpar@9: 7 3 3.2 alpar@9: 8 3.5 2 alpar@9: 9 4 2.7 alpar@9: 10 4.5 3.5 alpar@9: 11 5 1 alpar@9: 12 5.5 4 alpar@9: 13 6 3.6 alpar@9: 14 6.6 2.7 alpar@9: 15 7 5.7 alpar@9: 16 7.6 4.6 alpar@9: 17 8.5 6 alpar@9: 18 9 6.8 alpar@9: 19 10 7.3 alpar@9: ; alpar@9: alpar@9: end;