#Md. Alamgir Hossain
#Dept. of Computer Science & Engineering
#Jessore University of Science & Technology
#Secent method implementation us ing perl language......
#--------------------The given function ---------------------------
sub secentMethod
{
($x)=@_;
#return (($x*$x)-(5*$x)+2);
#return (2*$x-3-cos($x));
#return (($x*$x*$x) - (21*$x)+3500);
#return ($x*$x-5*$x+2);
#return ($x*$x)-(4*$x)-10;
return ($x*$x*$x)-(4*$x)-9;
}
$x0,$x1,$l1,$l2,$r,$f1,$f2,$f3,$iteration=5,$cnt=1;
#--------------------Finding a and b----------------------
for($k=-20;$k<=$iteration;$k++){
if(&secentMethod($k)>0){
if(&secentMethod($k+1)<0){
$x0=$k;
$x1=$k+1;
print "a = ";
print ($x0);
print " and F(x) is positive. b = ";
print ($x1);
print ", and F(x) is Negative \n\n";
last;
}
}
if(&secentMethod($k)<0){
if(secentMethod($k+1)>0){
$x0=$k;
$x1=$k+1;
print "a = ";
print ($x0);
print " and F(x) is Negative. b = ";
print ($x1);
print " and F(x) is Positive \n\n";
last;
}
}
}
print "---------------------------Iteraation part-------------------------------\n\n";
$l1= $x0;
$l2= $x1;
#------Finding root after one by one iteration---------------
for($i=1;$i<=$iteration;$i++){
$first=(&secentMethod($l2)*($l2-$l1));
$second=(&secentMethod($l2)-&secentMethod($l1));
$l3=$l2-($first/$second);
#$l3=$l2- ((&secentMethod($l2)*($l2-$l1))/(&secentMethod($l2)-&secentMethod($l1)));
#$pr=abs(($l3-$l2)/($l3));
print "After iteration ";
print ($i);
print " The root is : ";
print ($l3);
print "\n";
$l1=$l2;
$l2=$l3;
}
#-----------------------------Approximate root or Ans root-----------------------------
print "\nApproximate root is : ";
print ($l3);
print "\n";
exit;
No comments:
Post a Comment