# ニュートン補間法 Newton Interpolation # 引数 X軸 Y軸 (\@X, \@Y) # 戻り値 ニュートン補間法 (@NewtonInterpolation) sub NEWTONINTERPOLATION{ my ($X, $Y) = @_; my @NewtonInterpolation = (); my @ForwardDifference = ($$Y[0]); my $count = @$X - 1; # 配列数の確認 if(($count < 0) || (@$X != @$Y)){ return 0; } # 前進差分 my @DiffY = @$Y; for(my $i = 0; $i < $count; $i++){ for(my $j = 0; $j <= $count - ($i + 1); $j++){ $DiffY[$j] = ($DiffY[$j + 1] - $DiffY[$j]) / ($$X[$i + $j + 1] - $$X[$j]); } # 前進差分 $ForwardDifference[$i + 1] = $DiffY[0]; } # 動作確認 Y = f(X) Y軸の値を求める my @Test = @$X; for(my $i = 0; $i < @Test; $i++){ my $x = $Test[$i]; my $sum = $ForwardDifference[0]; for(my $j = 1; $j <= $count; $j++){ my $Diffx_X = 1; for(my $k = 0; $k < $j; $k++){ $Diffx_X *= ($x - $$X[$k]); } $sum += $ForwardDifference[$j] * $Diffx_X; } # ニュートン補間法 Newton Interpolation $NewtonInterpolation[$i] = $sum; } return @NewtonInterpolation; }