1#!/usr/local/bin/ruby 2 3# 4# nlsolve.rb 5# An example for solving nonlinear algebraic equation system. 6# 7 8require "bigdecimal" 9require "bigdecimal/newton" 10include Newton 11 12class Function 13 def initialize() 14 @zero = BigDecimal::new("0.0") 15 @one = BigDecimal::new("1.0") 16 @two = BigDecimal::new("2.0") 17 @ten = BigDecimal::new("10.0") 18 @eps = BigDecimal::new("1.0e-16") 19 end 20 def zero;@zero;end 21 def one ;@one ;end 22 def two ;@two ;end 23 def ten ;@ten ;end 24 def eps ;@eps ;end 25 def values(x) # <= defines functions solved 26 f = [] 27 f1 = x[0]*x[0] + x[1]*x[1] - @two # f1 = x**2 + y**2 - 2 => 0 28 f2 = x[0] - x[1] # f2 = x - y => 0 29 f <<= f1 30 f <<= f2 31 f 32 end 33end 34 f = BigDecimal::limit(100) 35 f = Function.new 36 x = [f.zero,f.zero] # Initial values 37 n = nlsolve(f,x) 38 p x 39