#Md. Alamgir Hossain
#Dept. of Computer Science & Engineering
#Jessore University of Science & Technology
#Newton Rapson method implementation using perl language......
#---------------------------The given function----------------
sub Function
{
($x) = @_;
#Using return keyword you can check your desired function/equation............
#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;
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 ";
print "$i";
print " Root is : ";
print ($h);
print "\n";
$x0=$x1;
$root=$h;
$ite = $i;
if($h<.01){
last;
}
} print "\n";
print "After ";
print ($ite);
print " iteration the Approximate Root is : ";
print ($root);
exit;
#Dept. of Computer Science & Engineering
#Jessore University of Science & Technology
#Newton Rapson method implementation using perl language......
#---------------------------The given function----------------
sub Function
{
($x) = @_;
#Using return keyword you can check your desired function/equation............
#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;
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 ";
print "$i";
print " Root is : ";
print ($h);
print "\n";
$x0=$x1;
$root=$h;
$ite = $i;
if($h<.01){
last;
}
} print "\n";
print "After ";
print ($ite);
print " iteration the Approximate Root is : ";
print ($root);
exit;
No comments:
Post a Comment