Moved BibTeX comments from src/sawsim.bib to README.bibtex.
[sawsim.git] / testing / const_rate / fit_exponential
1 #!/usr/bin/python
2 #
3 # fit data coming in on stdin to an exponential decay
4 # output:
5 #     <convergence information>
6 #   initial value:\t<some number>
7 #   time constant:\t<another number>
8 #
9 # usage: fit_exponential
10 # for example:
11 #   $ cat datafile | fit_exponential
12
13 import sys
14 sys.path.append('../common/')
15 import fit
16 from scipy import exp
17
18 class Model (fit.Model) :
19     def printModel(self) :
20         print "a*exp(x/b)"
21     def printParams(self, params) :
22         print "initial value:\t%g" % params[0]
23         print "time constant:\t%g" % params[1]
24     def model(self, x, params) :
25         a = params[0]
26         b = params[1]
27         return a*exp(x/b)
28     def guessInitialParams(self) :
29         init = self.data[0]
30         fin = self.data[-1]
31         slope = (fin[1]-init[1])/(fin[0]-init[0])
32         return (init[1],slope)
33
34 if __name__ == "__main__" :
35     fit.run(2, Model)