30
|
1 |
#======================================================================
|
|
2 |
# . L S F I T . P O L Y
|
|
3 |
# doc: Wed Feb 24 09:40:06 1999
|
36
|
4 |
# dlm: Sun May 13 08:25:37 2018
|
30
|
5 |
# (c) 1999 A.M. Thurnherr
|
36
|
6 |
# uE-Info: 114 36 NIL 0 0 72 2 2 4 NIL ofnI
|
30
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# What you need to provide if you wanna fit a different
|
|
10 |
# linear model function to your data:
|
|
11 |
# - a number of global variables to be set during loading
|
|
12 |
# - a number of subs to perform admin tasks (usage, init, ...)
|
|
13 |
# - a sub to evaluate the basis funs at a given x value; each
|
|
14 |
# y value must be stored in @A (beginning with
|
|
15 |
# A[1]!!!).
|
|
16 |
# - the interface is documented between +++++++ lines
|
|
17 |
|
|
18 |
# fit polynomial (sum of A_i x^i) to data
|
|
19 |
# NB: - preferable to [./.lmfit.poly]
|
|
20 |
|
|
21 |
# HISTORY:
|
|
22 |
# Jul 31, 1999: - adapted from [./.lmfit.poly]
|
|
23 |
# Aug 01, 1999: - changed &modelEvaluate() interface
|
|
24 |
# Aug 02, 1999: - added &antsDescription()
|
|
25 |
# Mar 17, 2001: - param->arg
|
|
26 |
# Jul 12, 2004: - made poly-order argument into -o option
|
|
27 |
# Jul 28, 2006: - Version 3.3 [HISTORY]
|
|
28 |
# Sep 19, 2011: - moved part of the usage code into init() to allow use in [pgram]
|
|
29 |
# Jan 10, 2013: - added extremum output when fitting parabola (-o 2)
|
36
|
30 |
# May 13, 2018: - BUG: replaced opt_o with modelNFit in &modelCleanup()
|
30
|
31 |
|
|
32 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
33 |
#
|
|
34 |
# THE FOLLOWING VARIABLES MUST BE SET GLOBALLY (i.e. during loading)
|
|
35 |
#
|
|
36 |
# $modelOpts string of allowed options
|
|
37 |
# $modelOptsUsage usage information string for options
|
|
38 |
# $modelMinArgs min # of arguments of model
|
|
39 |
# $modelArgsUsage usage information string for arguments
|
|
40 |
#
|
|
41 |
# The following variables may be set later but not after &modelInit()
|
|
42 |
#
|
|
43 |
# $modelNFit number of params to fit in model
|
|
44 |
# @nameA symbolic names of model parameters
|
|
45 |
#
|
|
46 |
# You should call &antsDescription() for the -c option here
|
|
47 |
#
|
|
48 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
49 |
|
|
50 |
$modelOpts = "o:";
|
|
51 |
$modelOptsUsage = "-o)rder <n>";
|
|
52 |
$modelMinArgs = 0;
|
|
53 |
$modelArgsUsage = "";
|
|
54 |
|
|
55 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
56 |
#
|
|
57 |
# &modelUsage() mangle parameters; NB: there may be `infinite' # of
|
|
58 |
# filenames after model arguments; this usually sets
|
|
59 |
# @A (the model parameters) but these can later be
|
|
60 |
# calculated heuristically during &modelInit()
|
|
61 |
#
|
|
62 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
63 |
|
|
64 |
sub modelUsage()
|
|
65 |
{
|
|
66 |
my($c);
|
|
67 |
|
|
68 |
$modelNFit = &antsCardOpt($opt_o) + 1; # order of polynomial
|
|
69 |
die("$0 (.lsfit.poly): ERROR! -o required\n")
|
|
70 |
unless (defined($opt_o) && $opt_o >= 0);
|
|
71 |
&antsUsageError() unless ($#ARGV < 0 || -r $ARGV[0]);
|
|
72 |
}
|
|
73 |
|
|
74 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
75 |
#
|
|
76 |
# &modelInit() initializes model after reading of data
|
|
77 |
#
|
|
78 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
79 |
|
|
80 |
sub modelInit()
|
|
81 |
{
|
|
82 |
for ($c=0; $c<$modelNFit; $c++) { # init coefficients
|
|
83 |
$A[$c+1] = nan;
|
|
84 |
$nameA[$c+1] = "c$c"; # and names
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
89 |
#
|
|
90 |
# &modelEvaluate(idx,xfnr,vals) evaluate polynomial basis funs at x
|
|
91 |
# idx current index in @ants_
|
|
92 |
# xfnr field number of x field
|
|
93 |
# vals reference to return values (1-relative!)
|
|
94 |
#
|
|
95 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
96 |
|
|
97 |
sub modelEvaluate($$$)
|
|
98 |
{
|
|
99 |
my($idx,$xfnr,$valsR) = @_;
|
|
100 |
my($i);
|
|
101 |
|
|
102 |
$valsR->[1] = 1;
|
|
103 |
for ($i=2; $i<=$#{$valsR}; $i++) {
|
|
104 |
$valsR->[$i] = $valsR->[$i-1] * $ants_[$idx][$xfnr];
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
109 |
# &modelCleanup() cleans up after fitting but before output
|
|
110 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
111 |
|
|
112 |
sub modelCleanup()
|
|
113 |
{
|
36
|
114 |
return unless ($0 eq 'lsfit' && $modelNFit == 3); # calculate loc of extremum on parabolic fits with lsfit only
|
30
|
115 |
|
|
116 |
my($extX) = -$A[2] / (2 * $A[3]);
|
|
117 |
if ($A[3] > 0) {
|
|
118 |
&antsInfo(".lsfit.poly: minimum at %.1f",$extX);
|
|
119 |
} elsif ($A[3] < 0) {
|
|
120 |
&antsInfo(".lsfit.poly: maximum at %.1f",$extX);
|
|
121 |
} else {
|
|
122 |
&antsInfo(".lsfit.poly: saddle point at %.1f",$extX);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
1;
|