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

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).

Examples:

fit(some_data, function: 'exp(a/x)', initials: {a: 10}, term_option: { xrange: 1..100 })
fit(some_dataset, using: '1:2:3')

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: {}, **options)
  dataset = data.is_a?(Dataset) ? Dataset.new(data.data) : Dataset.new(data)
  opts_str = OptionHandling.ruby_class_to_gnuplot(options)
  output = gnuplot_fit(function, dataset, opts_str, initials, term_options)
  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).

Examples:

fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'yscale * (yoffset + exp((x - xoffset) / xscale))',
#=>   initals: { yoffset: -11, xoffset: 0.1, yscale: 1, xscale: 1 },
#=>   term_option: { xrange: 1..100 }
#=> )
fit_log(...)
fit_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, **options|
    options[:initials] = {
      yoffset: 0.1,
      xoffset: 0.1,
      yscale: 1,
      xscale: 1
    }.merge(options[:initials] || {})
    function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))"
    fit(data, **options, function: function)
  end
end

#fit_log(data, **options) ⇒ Hash

Shortcuts for fitting with several math functions (exp, log, sin).

Examples:

fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'yscale * (yoffset + exp((x - xoffset) / xscale))',
#=>   initals: { yoffset: -11, xoffset: 0.1, yscale: 1, xscale: 1 },
#=>   term_option: { xrange: 1..100 }
#=> )
fit_log(...)
fit_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, **options|
    options[:initials] = {
      yoffset: 0.1,
      xoffset: 0.1,
      yscale: 1,
      xscale: 1
    }.merge(options[:initials] || {})
    function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))"
    fit(data, **options, 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.

Examples:

fit_poly(some_data, degree: 5, initials: { a4: 10, a2: -1 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'a5*x**5 + a4*x**4 + ... + a0*x**0',
#=>   initals: {a5: 1, a4: 10, a3: 1, a2: -1, a1: 1, a0: 1},
#=>   term_option: { xrange: 1..100 }
#=> )

76
77
78
79
80
81
82
83
# File 'lib/gnuplotrb/fit.rb', line 76

def fit_poly(data, degree: 2, **options)
  sum_count = degree + 1
  initials = {}
  sum_count.times { |i| initials["a#{i}".to_sym] = 1 }
  options[:initials] = initials.merge(options[:initials] || {})
  function = sum_count.times.map { |i| "a#{i}*x**#{i}" }.join(' + ')
  fit(data, **options, function: function)
end

#fit_sin(data, **options) ⇒ Hash

Shortcuts for fitting with several math functions (exp, log, sin).

Examples:

fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'yscale * (yoffset + exp((x - xoffset) / xscale))',
#=>   initals: { yoffset: -11, xoffset: 0.1, yscale: 1, xscale: 1 },
#=>   term_option: { xrange: 1..100 }
#=> )
fit_log(...)
fit_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, **options|
    options[:initials] = {
      yoffset: 0.1,
      xoffset: 0.1,
      yscale: 1,
      xscale: 1
    }.merge(options[:initials] || {})
    function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))"
    fit(data, **options, function: function)
  end
end