Newton Raphson method in perl language


#Newton Rapson method implementation using perl language......
=comment
    Alamgir Hossain, CSE, JUST
=cut
#---------------------------The given function----------------
sub Function
{
  ($x) = @_;
  return ($x*$x)-2*$x;
  #return (2*$x)-3*sin($x)-5;
}
#--------------------------Drive Function-----------------
sub DriveFunc
{
  ($x) = @_;
  return (2*$x-2);
  #return 2-3*cos($x);
}

$iteration = 10,$x0 = 10,$h=0;#You can change the iteration & x0 values-----
print "--------One by one iteration and root value chart--------\n\n";
$root, $ite;
for($i=1;$i<=$iteration;$i++)
{
        $h=&Function($x0)/&DriveFunc($x0);
        $x1 = $x0 - $h;
        print "After iteration $i the Root is : $h\n";
        $x0=$x1;
        $root=$h;
        $ite = $i;
        if($h<.01){
        last;
    }

   print "\nAfter $ite iteration the Approximate Root is : $root\n\n";
   print "-------------------Thank You---------------------\n\n";
exit;

No comments:

Post a Comment