/[ascend]/trunk/models/johnpye/leastsq.a4c
ViewVC logotype

Contents of /trunk/models/johnpye/leastsq.a4c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2618 - (show annotations) (download) (as text)
Fri May 11 20:44:49 2012 UTC (8 years, 4 months ago) by jpye
File MIME type: text/x-ascend
File size: 1494 byte(s)
Use a statement instead of variable for SSE.
1 (*
2 Testing implementation of a least-squares curve fit using IPOPT as the
3 solver...
4
5 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
11 REQUIRE "atoms.a4l";
12
13 (*
14 Model containing just the parameters we wish to identify.
15 *)
16 MODEL params;
17 a,b,c IS_A solver_var;
18 END params;
19
20 (*
21 Parametric model containing the fitting curve. The parameters are passed
22 in along with a single 'x' value; the 'y' value is calculated and stored
23 as part of this sub-model.
24 *)
25 MODEL fn(
26 p WILL_BE params;
27 x WILL_BE solver_var;
28 );
29 y IS_A solver_var;
30 y = (p.a * x + p.b)*x + p.c;
31 END fn;
32
33 (*
34 Least-squares curve fit model (fitting simple x-y data). No provision yet
35 for reading x-y data from an external file.
36 *)
37 MODEL leastsq;
38 p IS_A params;
39 n IS_A integer_constant;
40 n :== 10;
41 x[1..n], y[1..n] IS_A solver_var;
42 FOR i IN [1..n] CREATE
43 f[i] IS_A fn(p, x[i]);
44 END FOR;
45 obj: MINIMIZE SUM[(y[i] - f[i].y)^2 | i IN [1..n]];
46 METHODS
47 METHOD on_load;
48 FIX x[1..n], y[1..n];
49 FOR i IN [1..n] DO
50 x[i] := i;
51 END FOR;
52 y[1] := 27.5638512764;
53 y[2] := 17.0591647169;
54 y[3] := 10.2458879212;
55 y[4] := 5.5211061304;
56 y[5] := 3.8450975241;
57 y[6] := 2.7473614207;
58 y[7] := 4.279372646;
59 y[8] := 6.2993978336;
60 y[9] := 11.9244818208;
61 y[10] := 19.6601520996;
62
63 SOLVER IPOPT;
64 OPTION hessian_approximation 'limited-memory';
65 SOLVE;
66 RUN plot_fit;
67 END on_load;
68 METHOD plot_fit;
69 EXTERNAL leastsq_plot(SELF);
70 END plot_fit;
71 END leastsq;

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22