Module: GnuplotRB::Fit
- Defined in:
- lib/gnuplotrb/fit.rb
Overview
Contains methods relating to Gnuplot’s fit function. Covered in fit notebook.
You can also see original gnuplot’s fit in gnuplot doc p. 122.
Instance Method Summary collapse
-
#fit(data, function: 'a2*x*x+a1*x+a0', initials: { a2: 1, a1: 1, a0: 1 }, term_options: {}, **options) ⇒ Hash
Fit given data with function.
-
#fit_exp(data, **options) ⇒ Hash
Shortcuts for fitting with several math functions (exp, log, sin).
-
#fit_log(data, **options) ⇒ Hash
Shortcuts for fitting with several math functions (exp, log, sin).
-
#fit_poly(data, degree: 2, **options) ⇒ Hash
Shortcut for fit with polynomial.
-
#fit_sin(data, **options) ⇒ Hash
Shortcuts for fitting with several math functions (exp, log, sin).
Instance Method Details
#fit(data, function: 'a2*x*x+a1*x+a0', initials: { a2: 1, a1: 1, a0: 1 }, term_options: {}, **options) ⇒ Hash
Fit given data with function.
Fit waits for output from gnuplot Settings.max_fit_delay and throw exception if gets nothing. One can change this value in order to wait longer (if huge datasets is fitted).
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gnuplotrb/fit.rb', line 36 def fit(data, function: 'a2*x*x+a1*x+a0', initials: { a2: 1, a1: 1, a0: 1 }, term_options: {}, **) dataset = data.is_a?(Dataset) ? Dataset.new(data.data) : Dataset.new(data) opts_str = OptionHandling.ruby_class_to_gnuplot() output = gnuplot_fit(function, dataset, opts_str, initials, ) res = parse_output(initials.keys, function, output) { formula_ds: Dataset.new(res[2], title: 'Fit formula'), coefficients: res[0], deltas: res[1], data: dataset } end |
#fit_exp(data, **options) ⇒ Hash
Shortcuts for fitting with several math functions (exp, log, sin).
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gnuplotrb/fit.rb', line 119 %w(exp log sin).map do |fname| define_method("fit_#{fname}".to_sym) do |data, **| [:initials] = { yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }.merge([:initials] || {}) function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))" fit(data, **, function: function) end end |
#fit_log(data, **options) ⇒ Hash
Shortcuts for fitting with several math functions (exp, log, sin).
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gnuplotrb/fit.rb', line 119 %w(exp log sin).map do |fname| define_method("fit_#{fname}".to_sym) do |data, **| [:initials] = { yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }.merge([:initials] || {}) function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))" fit(data, **, function: function) end end |
#fit_poly(data, degree: 2, **options) ⇒ Hash
Shortcut for fit with polynomial. Degree here is max power of x in polynomial.
76 77 78 79 80 81 82 83 |
# File 'lib/gnuplotrb/fit.rb', line 76 def fit_poly(data, degree: 2, **) sum_count = degree + 1 initials = {} sum_count.times { |i| initials["a#{i}".to_sym] = 1 } [:initials] = initials.merge([:initials] || {}) function = sum_count.times.map { |i| "a#{i}*x**#{i}" }.join(' + ') fit(data, **, function: function) end |
#fit_sin(data, **options) ⇒ Hash
Shortcuts for fitting with several math functions (exp, log, sin).
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gnuplotrb/fit.rb', line 119 %w(exp log sin).map do |fname| define_method("fit_#{fname}".to_sym) do |data, **| [:initials] = { yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }.merge([:initials] || {}) function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))" fit(data, **, function: function) end end |