Class: ChebyRuby::FiniteDifferencing
- Inherits:
-
Object
- Object
- ChebyRuby::FiniteDifferencing
- 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
-
#func ⇒ UnivariateFunction
the function to which differentiation will be applied.
Instance Method Summary collapse
-
#backward(x, h = 0.1, order = 1) ⇒ Float
This does backward finite differencing.
-
#central(x, h = 0.1, order = 1) ⇒ Float
This does central finite differencing.
-
#forward(x, h = 0.1, order = 1) ⇒ Float
This does forward finite differencing.
-
#initialize(func) ⇒ FiniteDifferencing
constructor
Basic constructor for FiniteDifferencing.
Methods included from Differentiation
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.
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
#func ⇒ UnivariateFunction
the function to which differentiation will be applied
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).
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).
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).
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 |