Guass Backward Interpolation Formula Implementation Using Perl


#--------Guass Backward Interpolation Formula Implementation Using Perl-------------
#Md. Alamgir Hossain
#Dept. of Computer Science & Engineering
#Jessore University of Science & Technology
=comment
    Enter the number of records
    Now enter the value of x with corresponding y
    Now enter the value of X for finding y.
=cut

 sub fact
 {
  ($zz)=@_;
   $fct=1;
   for($i=1;$i<=$zz;$i++){
   $fct=$fct*$i;
  }
    return $fct;
 }
 print "Enter the number of records : \n";
$n = <>;

#Enter x with y

for($i=0;$i<$n;$i++)
{
    $x[$i] = <>;
    $y[0][$i] = <>;
}

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 "-------------Difference Table for Guass Backward Formula--------------------\n";
print "-----------------------------------------------------------\n";
print "  X\t";
for($i=0;$i<$n;$i++){
    print "Y($i)\t";
}
print "\n----------------------------------------------------------\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";
}
print "\n\n";
#-------Enter the finding value----------
$xn = <>;
$x0;
$x1;
$ii;
$i2;
for($i=0;$i<($n-1);$i++){
    if(($xn>=$x[$i])&&($xn<=$x[$i+1])){
        $ii = $i+1;
        $x0 = $x[$ii];
    }
}

$hv = $x[1]-$x[0];
$p = ($xn-$x0)/$hv;

$y0 = $y[0][$ii];
$y1 = $y[1][$ii-1];
$y2 = $y[2][$ii-1];
$y3 = $y[3][$ii-2];
$y4 = $y[4][$ii-2];
$y5 = $y[5][$ii-1];

$p1 = $p*($p+1);
$p2 = $p1*($p-1);
$p3 = $p2*($p+2);
$p4 = $p3*($p-2);

$f1 = ($p*$y1);
$f2 = ($p1*$y2)/&fact(2);
$f3 = ($p2*$y3)/&fact(3);
$f4 = ($p3*$y4)/&fact(4);
$f5 = ($p4*$y5)/&fact(5);

$f = $y0+$f1+$f2+$f3+$f4+$f5;
print "\nX0 is : $x0\n";
print "X is : $xn\n";
print "h is : $hv\n\n";
print "P is : $p\n";
print "\n\nFinal Answer for the given x is : $f\n\n\n\n";

=comment
    4
    2 3.818
    3 2.423
    4 -1.027
    5 -2.794
    3.5
  
=cut

No comments:

Post a Comment