Gregory-Newton Backward Interpolation Formula in perl language


#----------------Gregory-Newton Backward Interpolation Formula--------------
=comment
----------------Input Instructions-------------
    Md. Alamgir Hossain
    Dept. of Computer Science & Engineering
    Jessore University of Science & Technology.
=cut
=comment
----------------Input Instructions-------------
    At first enter the number of records
    Now enter the value of x with corresponding y
    Now enter the finding value of x---------
=cut

sub fact
{
    ($n)=@_;
    $fct=1;
    for($i=1;$i<=$n;$i++){
        $fct=$fct*$i;
    }
    return $fct;
}
sub fnc
{
    ($pp,$nn)=@_;
    $sum=$pp;
    for($i=1;$i<$nn;$i++){
        $sum=$sum*($pp+$i);
    }
    return $sum;
}
    print "Enter how many record you want to enter : \n";
    $n = <>;
    for($i=0;$i<$n;$i++){
        print "Enter X($i) and Y($i) : \n";
        $x[$i] = <>;
        $y[0][$i] = <>;
    }
    print "Enter the finding value : ";
  
    $f = <>;
     for($i=1;$i<$n;$i++){
        for($j=0;$j<($n-$i);$j++){
             $y[$i][$j]=$y[$i-1][$j+1]-$y[$i-1][$j];
        }
    }

    print "Newtons Backword Difference table : \n";
    print "---------------------------------------------------------\n";
    print "X\t";
    for($i=0;$i<$n;$i++){
        print "Y($i)\t";
    }
    print "\n";
    for($i=0;$i<$n;$i++){
      
        printf("\n%.3lf\t",$x[$i]);
        for($j=0;$j<($n-$i);$j++){
            printf ("%.3lf\t",$y[$j][$i]);
        }
        print "\n";
    }
    $xn = $x[$n-1];
    $h = $x[1]-$x[0];
    $p=(($f-$xn)/($h));

    print "----------------------------------------------------------\n";
    printf ("\n\nXn is : %.3lf\n",$xn);
    printf ("Height is : %.3lf\n",$h);
  
    printf ("Finding X is : %.3lf\n",$f);
    printf ("P is : %.3lf\n",$p);
  
    @arr;
    $w = $n-1;
    $q=0;
    for($i=0;$i<$n;$i++){
        $arr[$i] = $y[$q][$w];
        $q++;
        $w--;
    }

    for($i=0;$i<$n;$i++){
        printf("%.3lf\t",$arr[$i]);
    }
  
    print "\n";
    $fnd=0;
    for($ii=$n-1;$ii>=2;$ii--){
        $sum=(&fnc($p,$ii)*$arr[$ii])/(&fact($ii));
        $fnd = $fnd+$sum;
    }
  
    $ans=$arr[0]+$arr[1]*$p;
    $final_ans=$fnd+$ans;
    printf ("\n\nSo the Ans is : %.3lf\n",$final_ans);
  
    print "------------Thank You---------\n\n";
  
    #All source code : https://alamgirhossainjust.blogspot.com/
  
=comment
--------------Input----------
    5
    140 3.685
    150 4.854
    160 6.302
    170 8.076
    180 10.225  
=cut
   

No comments:

Post a Comment