#/*********************************************************** # newton.c -- Newton (ニュートン) 補間 #***********************************************************/ N = 5 $x = [ 0, 10, 20, 30, 40 ] $y = [ 610.66, 1227.4, 2338.1, 4244.9, 7381.2 ] # データ $a = []; N.times { $a.push(0) } # 補間多項式の係数 def maketable $w = []; N.times { $w.push(0) } for i in 0...N $w[i] = $y[i] (i - 1).downto(0) do |j| $w[j] = ($w[j + 1] - $w[j]) / ($x[i] - $x[j]) end $a[i] = $w[0] end end def interpolate( t ) p = $a[N - 1] (N - 2).downto(0) do |i| p = p * (t - $x[i]) + $a[i] end return p end maketable for i in 10..30 printf("%3d %6.1f\n", i, interpolate(i)) end exit 0