.lmfit.sqrt
changeset 39 56bdfe65a697
equal deleted inserted replaced
38:15c603bc4f70 39:56bdfe65a697
       
     1 #======================================================================
       
     2 #                    . L M F I T . S Q R T 
       
     3 #                    doc: Fri Oct 10 15:50:42 2014
       
     4 #                    dlm: Sat Oct 11 09:30:48 2014
       
     5 #                    (c) 2014 A.M. Thurnherr
       
     6 #                    uE-Info: 27 32 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 square root A[1]*sqrt(x) to data
       
    20 #
       
    21 # NOTES:
       
    22 #	- initial parameter estimates may be important
       
    23 #	- there is currently no heuristics
       
    24 
       
    25 # HISTORY:
       
    26 #	Oct 10, 2014: - created from [.lmfit.exp]
       
    27 #	Oct 11, 2014: - made it work
       
    28 
       
    29 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    30 #
       
    31 # THE FOLLOWING VARIABLES MUST BE SET GLOBALLY (i.e. during loading)
       
    32 #
       
    33 #	$modelOpts			string of allowed options
       
    34 #	$modelOptsUsage		usage information string for options
       
    35 #	$modelMinArgs		min # of arguments of model
       
    36 #	$modelArgsUsage		usage information string for arguments
       
    37 #
       
    38 # The following variables may be set later but not after &modelInit()
       
    39 #
       
    40 #	$modelNFit			number of params to fit in model
       
    41 #	@nameA				symbolic names of model parameters
       
    42 #
       
    43 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    44 
       
    45 $modelOpts = "";
       
    46 $modelOptsUsage = "";
       
    47 $modelMinArgs = 0;
       
    48 $modelArgsUsage = "[scale guess]";
       
    49 $modelNFit = 1;
       
    50 $nameA[1] = "scale";
       
    51 
       
    52 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    53 #
       
    54 # &modelUsage()		mangle parameters; NB: there may be `infinite' # of
       
    55 #					filenames after model arguments; this usually sets
       
    56 #					@A (the model parameters) but these can later be
       
    57 #					calculated heuristically during &modelInit()
       
    58 #
       
    59 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    60 
       
    61 sub modelUsage()
       
    62 {
       
    63 	$A[1] = nan; 			
       
    64 	$A[1] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
       
    65 	&antsUsageError() unless ($#ARGV < 0 || -r $ARGV[0]);
       
    66 }
       
    67 
       
    68 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    69 #
       
    70 # &modelInit()		initializes model after reading of data
       
    71 #
       
    72 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    73 
       
    74 sub modelInit()
       
    75 {
       
    76 	$A[1] = 1 unless (numberp($A[1]));		# scale
       
    77 }
       
    78 
       
    79 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    80 #
       
    81 # &modelEvaluate(x,A,dyda)	evaluate polynom and derivatives
       
    82 #		x					x value (NOT xfnr)
       
    83 #		A					reference to @A
       
    84 #		dyda				reference to array for partial derivatives
       
    85 #							(wrt individaul params in @A)
       
    86 #		<ret val>			y value
       
    87 #
       
    88 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    89 
       
    90 sub modelEvaluate($$$)					# y  = A[1]*sqrt(*x)
       
    91 {										
       
    92 	my($x,$AR,$dydaR) = @_;
       
    93 	my($v) = sqrt($x);
       
    94 
       
    95 	$dydaR->[1] = $v;					# dy/dA[1] = sqrt(x)
       
    96 	return $AR->[1]*$v;
       
    97 }
       
    98 
       
    99 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
   100 # &modelCleanup()	cleans up after fitting but before output
       
   101 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
   102 
       
   103 sub modelCleanup()
       
   104 {
       
   105 }