Stirling interpolaltion formula implementation using perl language


#----------Stirlings Interpolation Formula Implementation Using Perl----------
=comment
    Md. Alamgir Hossain
   Dept. of Computer Science & Engineering
  Jessore University of Science & Technology
=cut
=comment
------------------Input Instruction---------
    Enter the number of records
    Now enter the value of x with corresponding y
    Now enter the value of X for finding y.
=cut
#Main code start from here----------
#Sub function fact for finding factorial----------
 sub fact
 {
  ($zz)=@_;
   $fct=1;
   for($i=1;$i<=$zz;$i++){
   $fct=$fct*$i;
  }
    return $fct;
 }
 print "Enter how many records : \n";
$n = <>;
#Enter x with y
print "Enter the value of x with corresponding y : \n";
for($i=0;$i<$n;$i++)
{
    $x[$i] = <>;#For x
    $y[0][$i] = <>;#For y
}
print "Enter the finding x : \n";
$xn = <>;
#Mechanism for central difference table----
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---------
print "--------Central Difference table for Stirlings Interpolation------------\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]);#print value of x
   
    for($j=0;$j<($n-$i);$j++){
        printf ("%.3lf\t",$y[$j][$i]);#print y
    }
    print "\n";
}
print "\n\n";

$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);#finding p

$hv = $x1-$x0;

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

$p1 = $p*$p;
$p2 = $p*(($p*$p)-1);
$p3 = $p1*(($p*$p)-1);
$p4 = $p2*($p1-4);
$p5 = $p3*($p1-4);

$f1 = ($p*($y1+$y11))/2;
$f2 = ($p1/2)*$y2;
$f3 = ($p2/&fact(3))*(($y3+$y31)/2);
$f4 = ($p3/&fact(4))*($y4);
$f5 = ($p4/&fact(5))*(($y5+$y51)/2);
$f6 = ($p5/&fact(6))*$y6;

$f = $y0+$f1+$f2+$f3+$f4+$f5+$f6;#Final law
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 is : $f\n\n";

print "-------------Thank You----------\n\n";

=comment
--------------Input Example-----------
    5
    2 5
    4 49
    6 181
    8 449
    10 901
    7
    Final Answer is : 295
=cut

No comments:

Post a Comment