//Operating system: Windows 8
//SCILAB Ver: 5.5.2
//Expriment No. 1
//Objective: Determination of largest and smallest (in magnitude) Eigen value & 
//Eigen Vectors Using Power Method and Inverse Power Method respectively.


//Enter the no dimension of a sqaure of matrix A: 3
//Enter the element no (1,1):2
//Enter the element no (1,2):1
//Enter the element no (1,3):1
//Enter the element no (2,1):1
//Enter the element no (2,2):2
//Enter the element no (2,3):1
//Enter the element no (3,1):1
//Enter the element no (3,2):1
//Enter the element no (3,3):5
//Let Matrix A is A=[2,1,1;1,2,1;1,1,5];

clc
clear
//*****************************************************************
// Creating an input square matrix 
//*****************************************************************
m = input("Enter the dimension of row of square matrix A:   ")

for i=1:m
    for j=1:m
        mprintf("Enter the element no (%d,%d):  ",i,j)
        A(i,j)=input("")
    end
end

//*****************************************************************
// Creating initial approximation x0
//*****************************************************************
x=rand(m,1)

//*****************************************************************
//Finding smallest Eigen Value using Inverse Power Method
//*****************************************************************
z=1
f=1
y0=rand(m,1)
while(f>0.00001)
  y1=inv(A)*y0
  lowest=norm(y1,2)
  y0=y1/lowest
  f=abs(z-lowest)
  z=lowest
end

disp(lowest,'Lowest eigen value')
disp(y0,'Corresponding eigen vector')

//*****************************************************************
//Finding largest Eigen Value using Power Method
//*****************************************************************
x0=rand(m,1)
y=1
d=1
while (d>0.00001)
  x1=A*x0
  highest=norm(x1,2)
  x0=x1/highest
  d=abs(y-highest)
  y=highest
end
disp(highest,'Largest Eigen Value')
disp(x0,'Corresponding Eigen Vector')


