1
1
module GnuplotRB
2
2
##
3
- # Contains methods relating to Gnuplot's fit function.
3
+ # Contains methods relating to Gnuplot's fit function. Covered in
4
+ # {fit notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/fitting_data.ipynb].
5
+ #
6
+ # You can also see original gnuplot's fit in gnuplot
7
+ # doc - http://www.gnuplot.info/docs_5.0/gnuplot.pdf p. 122.
4
8
module Fit
5
9
##
6
- # ====== Overview
7
- # Fit given data with function. Covered in
8
- # {fit notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/fitting_data.ipynb].
9
- # ====== Arguments
10
- # * *data* - method accepts the same sources as Dataset.new
10
+ # Fit given data with function.
11
+ #
12
+ # Fit waits for output from gnuplot Settings.max_fit_delay and throw exception if gets nothing.
13
+ # One can change this value in order to wait longer (if huge datasets is fitted).
14
+ #
15
+ # @param data [#to_gnuplot_points] method accepts the same sources as Dataset.new
11
16
# and Dataset object
12
- # * *:function* - function to fit data with. Default 'a2*x*x+a1*x+a0'
13
- # * *:initials* - initial values for coefficients used in fitting.
14
- # Default: {a2: 1, a1: 1, a0: 1}
15
- # * *:term_options* - terminal options that should be setted to terminal before fit.
16
- # You can see them in Plot's documentation (or even better in gnuplot doc).
17
+ # @param :function [String] function to fit data with
18
+ # @param :initials [Hash] initial values for coefficients used in fitting
19
+ # @param :term_options [Hash] terminal options that should be setted to terminal before fit.
20
+ # You can see them in Plot's documentation (or even better in gnuplot doc)
17
21
# Most useful here are ranges (xrange, yrange etc) and fit option which tunes fit parameters
18
- # (see { gnuplot doc}[ http://www.gnuplot.info/docs_5.0/gnuplot.pdf] p. 122).
19
- # * * options* - options passed to Gnuplot's fit such as *using*. They are covered in
20
- # { gnuplot doc}[ http://www.gnuplot.info/docs_5.0/gnuplot.pdf] (pp. 69-74)
21
- # ====== Return value
22
- # Fit returns hash of 4 elements:
23
- # * * :formula_ds* - dataset with best fit curve as data
24
- # * * :coefficients* - hash of calculated coefficients. So if you gave {via: [:a, :b, :c]} or
25
- # { initials: {a: 1, b: 1, c: 1} } it will return hash with keys :a, :b, :c and its values
26
- # * * :deltas* - Gnuplot calculates possible deltas for coefficients during fitting and
27
- # * deltas* hash contains this deltas
28
- # * * :data* - pointer to Datablock with given data
29
- # ====== Examples
22
+ # (see gnuplot doc - http://www.gnuplot.info/docs_5.0/gnuplot.pdf p. 122)
23
+ # @param options [Hash] options passed to Gnuplot's fit such as *using*. They are covered in
24
+ # gnuplot doc - http://www.gnuplot.info/docs_5.0/gnuplot.pdf (pp. 69-74)
25
+ #
26
+ # @return [Hash] hash with four elements:
27
+ # - :formula_ds - dataset with best fit curve as data
28
+ # - :coefficients - hash of calculated coefficients. So if you gave
29
+ # ``{ initials: {a: 1, b: 1, c: 1} }`` it will return hash with keys :a, :b, :c and its values
30
+ # - :deltas - Gnuplot calculates possible deltas for coefficients during fitting and
31
+ # deltas hash contains this deltas
32
+ # - :data - pointer to Datablock with given data
33
+ # @example
30
34
# fit(some_data, function: 'exp(a/x)', initials: {a: 10}, term_option: { xrange: 1..100 })
31
35
# fit(some_dataset, using: '1:2:3')
32
36
def fit ( data , function : 'a2*x*x+a1*x+a0' , initials : { a2 : 1 , a1 : 1 , a0 : 1 } , term_options : { } , **options )
@@ -43,17 +47,24 @@ def fit(data, function: 'a2*x*x+a1*x+a0', initials: { a2: 1, a1: 1, a0: 1 }, ter
43
47
end
44
48
45
49
##
46
- # ====== Overview
47
50
# Shortcut for fit with polynomial. Degree here is max power of *x* in polynomial.
48
- # ====== Arguments
49
- # * *data* - method accepts the same sources as Dataset.new and Dataset object
50
- # * *:degree* - degree of polynomial
51
- # * *options* - all of this options will be passed to *#fit* so you
52
- # can set here any *term_options*. If you pass here *:initials* hash, it
53
- # will be merged into default initals hash (all values are 1).
54
- # ====== Return value
55
- # See the same section for #fit.
56
- # ====== Examples
51
+ #
52
+ # @param data [#to_gnuplot_points] method accepts the same sources as Dataset.new
53
+ # and Dataset object
54
+ # @param :degree [Integer] degree of polynomial
55
+ # @param options [Hash] all of this options will be passed to #fit so you
56
+ # can set here any options listed in its docs. If you pass here :initials hash, it
57
+ # will be merged into default initals hash. Formula by default is
58
+ # "xn*x**n + ... + x0*x**0", initials by default "{ an: 1, ..., a0: 1 }"
59
+ #
60
+ # @return [Hash] hash with four elements:
61
+ # - :formula_ds - dataset with best fit curve as data
62
+ # - :coefficients - hash of calculated coefficients. So for degree = 3
63
+ # it will return hash with keys :a3, :a2, :a1, :a0 and calculated values
64
+ # - :deltas - Gnuplot calculates possible deltas for coefficients during fitting and
65
+ # deltas hash contains this deltas
66
+ # - :data - pointer to Datablock with given data
67
+ # @example
57
68
# fit_poly(some_data, degree: 5, initials: { a4: 10, a2: -1 }, term_option: { xrange: 1..100 })
58
69
# #=> The same as:
59
70
# #=> fit(
@@ -72,22 +83,29 @@ def fit_poly(data, degree: 2, **options)
72
83
end
73
84
74
85
##
75
- # :method: fit_<function>
76
- # :call-seq:
77
- # fit_exp(data, **options) -> Hash
78
- # fit_log(data, **options) -> Hash
79
- # fit_sin(data, **options) -> Hash
86
+ # @!method fit_exp(data, **options)
87
+ # @!method fit_log(data, **options)
88
+ # @!method fit_sin(data, **options)
80
89
#
81
- # ====== Overview
82
90
# Shortcuts for fitting with several math functions (exp, log, sin).
83
- # ====== Arguments
84
- # * *data* - method accepts the same sources as Dataset.new and Dataset object
85
- # * *options* - all of this options will be passed to *#fit* so you
86
- # can set here any *term_options*. If you pass here *:initials* hash, it
87
- # will be merged into default initals hash { yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }
88
- # ====== Return value
89
- # See the same section for #fit.
90
- # ====== Examples
91
+ #
92
+ # @param data [#to_gnuplot_points] method accepts the same sources as Dataset.new
93
+ # and Dataset object
94
+ # @param options [Hash] all of this options will be passed to #fit so you
95
+ # can set here any options listed in its docs. If you pass here :initials hash, it
96
+ # will be merged into default initals hash. Formula by default is
97
+ # "yscale * (yoffset + #{function name}((x - xoffset) / xscale))", initials by default
98
+ # "{ yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }"
99
+ #
100
+ # @return [Hash] hash with four elements:
101
+ # - :formula_ds - dataset with best fit curve as data
102
+ # - :coefficients - hash of calculated coefficients. So for this case
103
+ # it will return hash with keys :yoffset, :xoffset, :yscale, :xscale and calculated values
104
+ # - :deltas - Gnuplot calculates possible deltas for coefficients during fitting and
105
+ # deltas hash contains this deltas
106
+ # - :data - pointer to Datablock with given data
107
+ #
108
+ # @example
91
109
# fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
92
110
# #=> The same as:
93
111
# #=> fit(
@@ -116,6 +134,9 @@ def fit_poly(data, degree: 2, **options)
116
134
##
117
135
# It takes some time to produce output so here we need
118
136
# to wait for it.
137
+ #
138
+ # Max time to wait is stored in Settings.max_fit_delay, so one
139
+ # can change it in order to wait longer.
119
140
def wait_for_output ( term , variables )
120
141
# now we should catch 'error' from terminal: it will contain approximation data
121
142
# but we can get a real error instead of output, so lets wait for limited time
0 commit comments