Class: ChebyRuby::FiniteDifferencing

Inherits:
Object
  • Object
show all
Includes:
Differentiation
Defined in:
lib/chebyruby/finite_differencing.rb

Overview

This is the class for finite differencing. Finite differencing is a popular method of finding a numeric derivative. There are three different implementations of the finite differencing method. There is the forward and backward implementations, both with an order of accuracy of O(h) and central differencing with an order of accuracy of O(h^2).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Differentiation

#slope

Constructor Details

#initialize(func) ⇒ FiniteDifferencing

Basic constructor for FiniteDifferencing. It can be implemented in two ways. It can either have a UnivariateFunction passed to it or a proc.

Parameters:



20
21
22
23
24
25
26
# File 'lib/chebyruby/finite_differencing.rb', line 20

def initialize(func)
  if func.is_a? Proc
    @func = UnivariateFunction.new(&func)
  else
    @func = func
  end
end

Instance Attribute Details

#funcUnivariateFunction

the function to which differentiation will be applied

Returns:



11
12
13
# File 'lib/chebyruby/finite_differencing.rb', line 11

def func
  @func
end

Instance Method Details

#backward(x, h = 0.1, order = 1) ⇒ Float

This does backward finite differencing. The formula being used is: <pre> Sum[(-1)^i * Binom(n i) * f(x - i*h), (i, 0, n)]/(h^n) </pre>

the smaller the better with exceptions especially when considering underflow).

Parameters:

  • x (Numeric)

    the value at which to find the derivative

  • h (Numeric) (defaults to: 0.1)

    the value to use for the differencing (generally

  • order (Integer) (defaults to: 1)

    the order of the derivative. (Ex. 1 for f’, 2 for f”, etc.)

Returns:

  • (Float)

    the backward finite differencing approximation of f^(n) (x)



55
56
57
58
59
60
# File 'lib/chebyruby/finite_differencing.rb', line 55

def backward(x, h = 0.1, order = 1)
  (0..order).map do |i|
    (-1)**i * FiniteDifferencing::binomial(order, i) *
    func.value(x - i * h)
  end.inject(:+) / (h**order).to_f
end

#central(x, h = 0.1, order = 1) ⇒ Float

This does central finite differencing. The formula being used is: <pre> Sum[(-1)^i * Binom(n i) * f(x + (n/2-i) * h), (i, 0, n)]/(h^n) </pre>

the smaller the better with exceptions especially when considering underflow).

Parameters:

  • x (Numeric)

    the value at which to find the derivative

  • h (Numeric) (defaults to: 0.1)

    the value to use for the differencing (generally

  • order (Integer) (defaults to: 1)

    the order of the derivative. (Ex. 1 for f’, 2 for f”, etc.)

Returns:

  • (Float)

    the central finite differencing approximation of f^(n) (x)



72
73
74
75
76
77
# File 'lib/chebyruby/finite_differencing.rb', line 72

def central(x, h = 0.1, order = 1)
  (0..order).map do |i|
    (-1)**i * FiniteDifferencing::binomial(order, i) *
    func.value(x + (order/2.0 - i) * h)
  end.inject(:+) / (h**order).to_f
end

#forward(x, h = 0.1, order = 1) ⇒ Float

This does forward finite differencing. The formula being used is: <pre> Sum[(-1)^i * Binom(n i) * f(x + (n-i) * h), (i, 0, n)]/(h^n) </pre>

the smaller the better with exceptions especially when considering underflow).

Parameters:

  • x (Numeric)

    the value at which to find the derivative

  • h (Numeric) (defaults to: 0.1)

    the value to use for the differencing (generally

  • order (Integer) (defaults to: 1)

    the order of the derivative. (Ex. 1 for f’, 2 for f”, etc.)

Returns:

  • (Float)

    the forward finite differencing approximation of f^(n) (x)



38
39
40
41
42
43
# File 'lib/chebyruby/finite_differencing.rb', line 38

def forward(x, h = 0.1, order = 1)
  (0..order).map do |i|
    (-1)**i * FiniteDifferencing::binomial(order, i) *
    func.value(x + (order - i) * h)
  end.inject(:+) / (h**order).to_f
end