Guass Forward Interpolation Formula implementation using perl language


# Md. Alamgir Hossain
#Dept. of Computer Science & Engineering
#Jessore University of Science & Technology

        #-----------------------Guass Forward Interpolation Formula Implementation Using Perl-------------

=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;
 }

$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 "-----------------------------------------------------------\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])){
        $x0 = $x[$i];
        $ii = $i;
        $x1 = $x[$i+1];
        $i2 = $i+1;
    }
}
$p = ($xn-$x0)/($x1-$x0);

$hv = $x1-$x0;

$y0 = $y[0][$ii];
$y1 = $y[1][$ii];
$y2 = $y[2][$ii-1];
$y3 = $y[3][$ii-1];
$y4 = $y[4][$ii-2];
$y5 = $y[5][$ii-2];
$p1 = $p*($p-1);
$p2 = $p1*($p+1);
$p3 = $p2*($p-2);
$p4 = $p3*($p+2);

$f1 = ($p*$y1);
$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";#Needed for Guass forward Difference
print "X is : $xn\n";
print "h is : $hv\n\n";
print "P is : $p\n";
print "\n\nFinal Answer is : $f\n\n\n\n";

No comments:

Post a Comment