Module: GnuplotRB

Defined in:
lib/gnuplotrb/version.rb,
lib/gnuplotrb/fit.rb,
lib/gnuplotrb/plot.rb,
lib/gnuplotrb/splot.rb,
lib/gnuplotrb/multiplot.rb,
lib/gnuplotrb/animation.rb,
lib/gnuplotrb/staff/dataset.rb,
lib/gnuplotrb/staff/settings.rb,
lib/gnuplotrb/staff/terminal.rb,
lib/gnuplotrb/staff/datablock.rb,
lib/gnuplotrb/mixins/plottable.rb,
lib/gnuplotrb/mixins/error_handling.rb,
lib/gnuplotrb/mixins/option_handling.rb

Overview

Overview

Ruby bindings for gnuplot.

Defined Under Namespace

Modules: ErrorHandling, OptionHandling, Plottable, Settings Classes: Animation, Datablock, Dataset, GnuplotError, Multiplot, Plot, Splot, Terminal

Constant Summary collapse

VERSION =
'0.3.0'

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) ⇒ Object

Overview

Fit given data with function. Covered in fit notebook.

Arguments
  • data - method accepts the same sources as Dataset.new and Dataset object

  • :function - function to fit data with. Default 'a2*x*x+a1*x+a0'

  • :initials - initial values for coefficients used in fitting. Default: 1, a1: 1, a0: 1

  • :via - coefficients that Gnuplot should change during fitting. Default: initials#keys

  • :term_options - terminal options that should be setted to terminal before fit. For example xrange, yrange etc

  • options - options passed to Gnuplot's fit such as using

Return value

Fit returns hash of 4 elements:

  • :formula_ds - dataset with best fit curve as data

  • :coefficients - hash of calculated coefficients. So if you gave [:a, :b, :c] or {a: 1, b: 1, c: 1 } it will return hash with keys :a, :b, :c and its values

  • :deltas - Gnuplot calculates possible deltas for coefficients during fitting and deltas hash contains this deltas

  • :data - pointer to Datablock with given data

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

27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gnuplotrb/fit.rb', line 27

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_poly(data, degree: 2, **options) ⇒ Object

Overview

Shortcut for fit with polynomial. Degree here is max power of x in polynomial.

Arguments
  • data - method accepts the same sources as Dataset.new and Dataset object

  • :degree - degree of polynomial

  • options - all of this options will be passed to #fit so you can set here any term_options. If you pass here :initials hash, it will be merged into default initals hash (all values are 1).

Return value

See the same section for #fit.

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 }
#=> )

60
61
62
63
64
65
66
67
# File 'lib/gnuplotrb/fit.rb', line 60

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

#fnameObject

:method: fit_<function> :call-seq: fit_exp(data, **options) -> Hash fit_log(data, **options) -> Hash fit_sin(data, **options) -> Hash

Overview

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

Arguments
  • data - method accepts the same sources as Dataset.new and Dataset object

  • options - all of this options will be passed to #fit so you can set here any term_options. If you pass here :initials hash, it will be merged into default initals hash { yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }

Return value

See the same section for #fit.

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

96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gnuplotrb/fit.rb', line 96

%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