# ニュートン補間法 Newton Interpolation # 引数 X軸 Y軸 X軸補間 (\@X, \@Y, \@PriceX) # 戻り値 ニュートン補間法 (@NewtonInterpolation) sub NEWTONINTERPOLATION{ my ($X, $Y, $PriceX) = @_; my @NewtonInterpolation = (); my @ForwardDifference = ($$Y[0]); my $CountX = @$PriceX - 1; my $count = @$X - 1; # 配列数の確認 if(($count < 0) || (@$X != @$Y) || (@$PriceX < 0)){ 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軸補間 for(my $i = 0; $i <= $CountX; $i++){ my $x = $$PriceX[$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; }