39
|
1 |
#======================================================================
|
|
2 |
# . L M F I T . E X P
|
|
3 |
# doc: Wed Feb 24 09:40:06 1999
|
|
4 |
# dlm: Fri Jul 28 13:40:56 2006
|
|
5 |
# (c) 1999 A.M. Thurnherr
|
|
6 |
# uE-Info: 30 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 exponential A[3]+A[2]*exp(A[1]*x) to data
|
|
20 |
#
|
|
21 |
# NOTES:
|
|
22 |
# - initial parameter estimates are crucial
|
|
23 |
# - there is currently no heuristics
|
|
24 |
|
|
25 |
# HISTORY:
|
|
26 |
# Mar 11, 1999: - created from [./.mfit.poly] & [./.mfit.gauss]
|
|
27 |
# Jul 31, 1999: - typecheck usage
|
|
28 |
# Mar 17, 2001: - param->arg
|
|
29 |
# Jan 16, 2006: - added notes
|
|
30 |
# Jul 28, 2006: - Version 3.3 [HISTORY]
|
|
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 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
47 |
|
|
48 |
$modelOpts = "";
|
|
49 |
$modelOptsUsage = "";
|
|
50 |
$modelMinArgs = 0;
|
|
51 |
$modelArgsUsage = "[exp [mul [add guess]]]";
|
|
52 |
$modelNFit = 3;
|
|
53 |
$nameA[1] = "exp";
|
|
54 |
$nameA[2] = "mul";
|
|
55 |
$nameA[3] = "add";
|
|
56 |
|
|
57 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
58 |
#
|
|
59 |
# &modelUsage() mangle parameters; NB: there may be `infinite' # of
|
|
60 |
# filenames after model arguments; this usually sets
|
|
61 |
# @A (the model parameters) but these can later be
|
|
62 |
# calculated heuristically during &modelInit()
|
|
63 |
#
|
|
64 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
65 |
|
|
66 |
sub modelUsage()
|
|
67 |
{
|
|
68 |
$A[1] = nan; $A[2] = nan; $A[3] = nan; # usage
|
|
69 |
$A[1] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
|
|
70 |
$A[2] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
|
|
71 |
$A[3] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
|
|
72 |
&antsUsageError() unless ($#ARGV < 0 || -r $ARGV[0]);
|
|
73 |
}
|
|
74 |
|
|
75 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
76 |
#
|
|
77 |
# &modelInit() initializes model after reading of data
|
|
78 |
#
|
|
79 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
80 |
|
|
81 |
sub modelInit()
|
|
82 |
{
|
|
83 |
$A[1] = 1 unless (numberp($A[1]));
|
|
84 |
$A[2] = 1 unless (numberp($A[2]));
|
|
85 |
$A[3] = 0 unless (numberp($A[3]));
|
|
86 |
}
|
|
87 |
|
|
88 |
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
89 |
#
|
|
90 |
# &modelEvaluate(x,A,dyda) evaluate polynom and derivatives
|
|
91 |
# x x value (NOT xfnr)
|
|
92 |
# A reference to @A
|
|
93 |
# dyda reference to array for partial derivatives
|
|
94 |
# (wrt individaul params in @A)
|
|
95 |
# <ret val> y value
|
|
96 |
#
|
|
97 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
98 |
|
|
99 |
sub modelEvaluate($$$) # y = A[3]+A[2]*exp(A[1]*x)
|
|
100 |
{
|
|
101 |
my($x,$AR,$dydaR) = @_;
|
|
102 |
my($e) = exp($AR->[1]*$x);
|
|
103 |
|
|
104 |
$dydaR->[1] = $AR->[2]*$x*$e;
|
|
105 |
$dydaR->[2] = $e;
|
|
106 |
$dydaR->[3] = 1; # partial derivatives
|
|
107 |
|
|
108 |
return $AR->[3] + $AR->[2]*$e;
|
|
109 |
}
|
|
110 |
|
|
111 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
112 |
# &modelCleanup() cleans up after fitting but before output
|
|
113 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
114 |
|
|
115 |
sub modelCleanup()
|
|
116 |
{
|
|
117 |
}
|