|
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; |
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; |
46 |
MINIMIZE sse; |
MINIMIZE sse; |
47 |
METHODS |
METHODS |
48 |
METHOD on_load; |
METHOD on_load; |
|
(*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[8] := 6.2993978336; |
y[8] := 6.2993978336; |
61 |
y[9] := 11.9244818208; |
y[9] := 11.9244818208; |
62 |
y[10] := 19.6601520996; |
y[10] := 19.6601520996; |
63 |
|
|
64 |
|
SOLVER IPOPT; |
65 |
|
OPTION hessian_approximation 'limited-memory'; |
66 |
|
SOLVE; |
67 |
|
RUN plot_fit; |
68 |
END on_load; |
END on_load; |
69 |
|
METHOD plot_fit; |
70 |
|
EXTERNAL leastsq_plot(SELF); |
71 |
|
END plot_fit; |
72 |
END leastsq; |
END leastsq; |
73 |
|
|