Module: Mathpack::NonlinearEquations

Defined in:
lib/mathpack/nonlinear_equations.rb

Class Method Summary collapse

Class Method Details

.derivative(xk, &function) ⇒ Object



34
35
36
37
# File 'lib/mathpack/nonlinear_equations.rb', line 34

def self.derivative(xk, &function)
  eps = 1e-5
  (function.call(xk + eps) - function.call(xk - eps)) / (2 * eps)
end

.invert(vector) ⇒ Object



26
27
28
# File 'lib/mathpack/nonlinear_equations.rb', line 26

def self.invert(vector)
  vector.map{ |element| -element }
end

.plus(first_array, second_array) ⇒ Object



30
31
32
# File 'lib/mathpack/nonlinear_equations.rb', line 30

def self.plus(first_array, second_array)
  Array.new(first_array.length) { |i| first_array[i] + second_array[i] }
end

.solve(params = {}, &function) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/mathpack/nonlinear_equations.rb', line 3

def self.solve(params = {}, &function)
  xk1 = params[:start]
  loop do
    xk = xk1
    xk1 = xk - function.call(xk) / derivative(xk, &function)
    break if (xk1 - xk).abs < params[:eps]
  end
  xk1
end

.solve_system(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mathpack/nonlinear_equations.rb', line 13

def self.solve_system(params = {})
  xk1 = params[:start].dup
  loop do
    xk = xk1.dup
    w_xk = params[:w_matrix].call(*xk, *params[:additional_params])
    f = params[:f].call(*xk, *params[:additional_params])
    delta = Mathpack::SLE.solve(matrix: w_xk, f: invert(f))
    xk1 = plus(xk, delta)
    break if Mathpack::IO.count_diff(xk1, xk) <= params[:eps]
  end
  xk1
end