#Power method for finding the eigen value and the eigen vector using Perl Language.........
=comment
Md. Alamgir Hossain
Dept. of Computer Science & Engineering
Jessore University of Science & Technology.
=cut
=comment
First Enter the order of your matrix.......
Now enter the matrix values by row wise one-by-one...
=cut
print"Enter the order of matrix : \n";
$n = <>;
print"Enter matrix elements row-wise one-by-one : \n";
for($i=1; $i<=$n; $i++)
{
for($j=1; $j<=$n; $j++)
{
$arr[$i][$j]=<>;
}
}
print"\nOur initial guess for the column vector is : 1,0,0 for 3 order matrix, 1,0 for 2 order matrix and so on.......\n";
$x[1] = 1;
for($k=2; $k<=$n; $k++)
{
$x[$k] = 0;
}
do
{
for($i=1; $i<=$n; $i++)
{
$z[$i]=0;
for($j=1; $j<=$n; $j++)
{
$z[$i] = $z[$i]+($arr[$i][$j]*$x[$j]);
}
}
$zmax=abs($z[1]);
for($i=2; $i<=$n; $i++)
{
if((abs($z[$i]))>$zmax){
$zmax=abs($z[$i]);
}
}
for($i=1; $i<=$n; $i++)
{
$z[$i] = $z[$i]/$zmax;
}
for($i=1; $i<=$n; $i++)
{
$e[$i]=0;
$e[$i]=abs((abs($z[$i]))-(abs($x[$i])));
}
$emax=$e[1];
for($i=2; $i<=$n; $i++)
{
if($e[$i]>$emax){
$emax=$e[$i];
}
}
for($i=1; $i<=$n; $i++)
{
$x[$i]=$z[$i];
}
}
while($emax>0.001);
print"\n The largest eigen value for the matrix is : ";
print "$zmax";
print"\n\nThe required eigen vector is :\n";
print "----------------------------------\n";
for($i=1; $i<=$n; $i++)
{
print "$z[$i]\n";
}
print "---------------------------------\n";
print "\n\n";
=comment
-----------------------------Input--------------
3
25
1
0
2
1
3
3
6
2
------------------------------------Output-------------------------
The largest eigen value for the matrix is : 25.1021914256925
The required eigen vector is :
----------------------------------
1
0.10239485477622
0.156370588030618
---------------------------------
=cut
=comment
Md. Alamgir Hossain
Dept. of Computer Science & Engineering
Jessore University of Science & Technology.
=cut
=comment
First Enter the order of your matrix.......
Now enter the matrix values by row wise one-by-one...
=cut
print"Enter the order of matrix : \n";
$n = <>;
print"Enter matrix elements row-wise one-by-one : \n";
for($i=1; $i<=$n; $i++)
{
for($j=1; $j<=$n; $j++)
{
$arr[$i][$j]=<>;
}
}
print"\nOur initial guess for the column vector is : 1,0,0 for 3 order matrix, 1,0 for 2 order matrix and so on.......\n";
$x[1] = 1;
for($k=2; $k<=$n; $k++)
{
$x[$k] = 0;
}
do
{
for($i=1; $i<=$n; $i++)
{
$z[$i]=0;
for($j=1; $j<=$n; $j++)
{
$z[$i] = $z[$i]+($arr[$i][$j]*$x[$j]);
}
}
$zmax=abs($z[1]);
for($i=2; $i<=$n; $i++)
{
if((abs($z[$i]))>$zmax){
$zmax=abs($z[$i]);
}
}
for($i=1; $i<=$n; $i++)
{
$z[$i] = $z[$i]/$zmax;
}
for($i=1; $i<=$n; $i++)
{
$e[$i]=0;
$e[$i]=abs((abs($z[$i]))-(abs($x[$i])));
}
$emax=$e[1];
for($i=2; $i<=$n; $i++)
{
if($e[$i]>$emax){
$emax=$e[$i];
}
}
for($i=1; $i<=$n; $i++)
{
$x[$i]=$z[$i];
}
}
while($emax>0.001);
print"\n The largest eigen value for the matrix is : ";
print "$zmax";
print"\n\nThe required eigen vector is :\n";
print "----------------------------------\n";
for($i=1; $i<=$n; $i++)
{
print "$z[$i]\n";
}
print "---------------------------------\n";
print "\n\n";
=comment
-----------------------------Input--------------
3
25
1
0
2
1
3
3
6
2
------------------------------------Output-------------------------
The largest eigen value for the matrix is : 25.1021914256925
The required eigen vector is :
----------------------------------
1
0.10239485477622
0.156370588030618
---------------------------------
=cut
No comments:
Post a Comment