 # Diff of /trunk/models/johnpye/leastsq.a4c

revision 2616 by jpye, Thu May 10 04:44:39 2012 UTC revision 2617 by jpye, Fri May 11 19:52:18 2012 UTC
# Line 1  Line 1
IMPORT "ipopt";
REQUIRE "atoms.a4l";

1  (*  (*
2      Testing implementation of a least-squares curve fit using IPOPT as the      Testing implementation of a least-squares curve fit using IPOPT as the
3      solver...      solver...
4
5      Solution should be close to a = 1, b = -12, c = 37      Solution should be close to a = 1, b = -12, c = 37
6  *)  *)
7    IMPORT "ipopt";
8    IMPORT "johnpye/extpy/extpy";
9    IMPORT "johnpye/leastsq_fit";
10    REQUIRE "atoms.a4l";
11
12    (*
13        Model containing just the parameters we wish to identify.
14    *)
15  MODEL params;  MODEL params;
16      a,b,c IS_A solver_var;      a,b,c IS_A solver_var;
17  END params;  END params;
18
19    (*
20        Parametric model containing the fitting curve. The parameters are passed
21        in along with a single 'x' value; the 'y' value is calculated and stored
22        as part of this sub-model.
23    *)
24  MODEL fn(  MODEL fn(
25      p WILL_BE params;      p WILL_BE params;
26      x WILL_BE solver_var;      x WILL_BE solver_var;
# Line 19  MODEL fn( Line 29  MODEL fn(
29      y = (p.a * x + p.b)*x + p.c;      y = (p.a * x + p.b)*x + p.c;
30  END fn;  END fn;
31
32    (*
33        Least-squares curve fit model (fitting simple x-y data). No provision yet
34        for reading x-y data from an external file.
35    *)
36  MODEL leastsq;  MODEL leastsq;
37      p IS_A params;      p IS_A params;
38      n IS_A integer_constant;      n IS_A integer_constant;
# Line 32  MODEL leastsq; Line 46  MODEL leastsq;
46      MINIMIZE sse;      MINIMIZE sse;
47  METHODS  METHODS
(*SOLVER ipopt;*)
49      FIX x[1..n], y[1..n];      FIX x[1..n], y[1..n];
50      FOR i IN [1..n] DO      FOR i IN [1..n] DO
51          x[i] := i;          x[i] := i;
60      y := 6.2993978336;      y := 6.2993978336;
61      y := 11.9244818208;      y := 11.9244818208;
62      y := 19.6601520996;      y := 19.6601520996;
63
64        SOLVER IPOPT;
65        OPTION hessian_approximation 'limited-memory';
66        SOLVE;
67        RUN plot_fit;