39
|
1 |
#======================================================================
|
|
2 |
# . L M F I T . P O L Y
|
|
3 |
# doc: Wed Feb 24 09:40:06 1999
|
|
4 |
# dlm: Fri Jul 28 13:35:50 2006
|
|
5 |
# (c) 1999 A.M. Thurnherr
|
|
6 |
# uE-Info: 28 41 NIL 0 0 72 2 2 4 NIL ofnI
|
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# What you need to provide if you wanna fit a different
|
|
10 |
# 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 model function which is to be fitted using
|
|
14 |
# a number of pararams which are all stored in @A (beginning at
|
|
15 |
# A[1]!!!). You also need to return the partial derivatives of
|
|
16 |
# the model function wrt all params.
|
|
17 |
# - the interface is documented between +++++++ lines
|
|
18 |
|
|
19 |
# fit polynomial (sum of A_i x^i) to data
|
|
20 |
# NB:
|
|
21 |
|
|
22 |
# HISTORY:
|
|
23 |
# Feb 25, 1999: - created
|
|
24 |
# Mar 14, 1999: - cosmetic changes
|
|
25 |
# Jul 31, 1999: - argument typechecking
|
|
26 |
# Mar 17, 2001: - param->arg
|
|
27 |
# Jan 12, 2006: - specify order with -o as in [.lsfit.poly]
|
|
28 |
# Jul 28, 2006: - Version 3.3 [HISTORY]
|
|
29 |
|
|
30 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
31 |
#
|
|
32 |
# THE FOLLOWING VARIABLES MUST BE SET GLOBALLY (i.e. during loading)
|
|
33 |
#
|
|
34 |
# $modelOpts string of allowed options
|
|
35 |
# $modelOptsUsage usage information string for options
|
|
36 |
# $modelMinArgs min # of arguments of model
|
|
37 |
# $modelArgsUsage usage information string for arguments
|
|
38 |
#
|
|
39 |
# The following variables may be set later but not after &modelInit()
|
|
40 |
#
|
|
41 |
# $modelNFit number of params to fit in model
|
|
42 |
# @nameA symbolic names of model parameters
|
|
43 |
#
|
|
44 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
45 |
|
|
46 |
$modelOpts = "o:";
|
|
47 |
$modelOptsUsage = "-o)rder <n>";
|
|
48 |
$modelMinArgs = 0;
|
|
49 |
$modelArgsUsage = "[c0 [c1 [...]]]";
|
|
50 |
|
|
51 |
&antsInfo("non-linear method deprecated; use `lsfit' instead");
|
|
52 |
|
|
53 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
54 |
#
|
|
55 |
# &modelUsage() mangle parameters; NB: there may be `infinite' # of
|
|
56 |
# filenames after model arguments; this usually sets
|
|
57 |
# @A (the model parameters) but these can later be
|
|
58 |
# calculated heuristically during &modelInit()
|
|
59 |
#
|
|
60 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
61 |
|
|
62 |
sub modelUsage()
|
|
63 |
{
|
|
64 |
my($c);
|
|
65 |
|
|
66 |
die("$0 (.lmfit.poly): ERROR! -o required\n") # order of polynomial
|
|
67 |
unless (defined($opt_o) && $opt_o >= 0);
|
|
68 |
$modelNFit = &antsCardOpt($opt_o)+1;
|
|
69 |
|
|
70 |
for ($c=0; $c<$modelNFit; $c++) { # init coefficients
|
|
71 |
if ($#ARGV >= 0 && ! -r $ARGV[0]) {
|
|
72 |
$A[$c+1] = &antsFloatArg();
|
|
73 |
} else {
|
|
74 |
$A[$c+1] = nan;
|
|
75 |
}
|
|
76 |
$nameA[$c+1] = "c$c"; # and names
|
|
77 |
}
|
|
78 |
&antsUsageError() unless ($#ARGV < 0 || -r $ARGV[0]);
|
|
79 |
}
|
|
80 |
|
|
81 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
82 |
#
|
|
83 |
# &modelInit() initializes model after reading of data
|
|
84 |
#
|
|
85 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
86 |
|
|
87 |
sub modelInit()
|
|
88 |
{
|
|
89 |
my($c);
|
|
90 |
|
|
91 |
for ($c=0; $c<$modelNFit; $c++) { # init coefficients
|
|
92 |
$A[$c+1] = 10**-$c unless (numberp($A[$c+1]));
|
|
93 |
}
|
|
94 |
|
|
95 |
}
|
|
96 |
|
|
97 |
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
98 |
#
|
|
99 |
# &modelEvaluate(x,A,dyda) evaluate polynomial and derivatives
|
|
100 |
# x x value (NOT xfnr)
|
|
101 |
# A reference to @A
|
|
102 |
# dyda reference to array for partial derivatives
|
|
103 |
# (wrt individaul params in @A)
|
|
104 |
# <ret val> y value
|
|
105 |
#
|
|
106 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
107 |
|
|
108 |
sub modelEvaluate($$$)
|
|
109 |
{
|
|
110 |
my($x,$AR,$dydaR) = @_;
|
|
111 |
my($i);
|
|
112 |
my($pow) = 1;
|
|
113 |
my($y) = 0;
|
|
114 |
|
|
115 |
for ($i=1; $i<=$modelNFit; $i++) {
|
|
116 |
$y += $AR->[$i]*$pow;
|
|
117 |
$dydaR->[$i] = $pow;
|
|
118 |
$pow *= $x;
|
|
119 |
}
|
|
120 |
return $y;
|
|
121 |
}
|
|
122 |
|
|
123 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
124 |
# &modelCleanup() cleans up after fitting but before output
|
|
125 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
126 |
|
|
127 |
sub modelCleanup()
|
|
128 |
{
|
|
129 |
}
|