You are on page 1of 10

#1

EIGEN VALUES AND EIGEN VECTOR USING THE POWER


METHOD :
INTRODUCTION:
EIGEN VALUES AND EIGEN VECTOR:
Eigen values are a special set of scalars associated with a linear system of equations that are
sometimes also known as characteristic roots, characteristic values which define the linear
transformation of the vector.
Let A be an n n matrix. The number is an eigenvalue ofA if there exists a non-zero vector x such
that Av= x
In this case, vector x is called an eigen vector of A corresponding to .
We can rewrite the condition Av= v as
(A− I)x=0
The equivalent matrix equation represents n homogenous linear equations which will have a non
trivial solution in the case |A− I|=0. On expansion it gives a set of equations in temrns of which
is called the characteristics equation whose roots are known as Eigen values.

POWER METHOD:

For large values of n, polynomial equations like are difficult and time-consuming to solve,
moreover, numerical techniques for approximating roots of polynomial equations of high degree are
sensitive to rounding errors. Thus power method can be used only to find the eigenvalue of A that is
largest in absolute value—this eigenvalue is called the dominant eigenvalue of A.
Let 1, 2, 3,….. n and be the eigenvalues of an n x n matrix A. 1 is called the
dominant eigenvalue of A if
| 1|>| i| (i =2,3,4……….n)
The eigenvectors corresponding to 1 are called dominant eigenvectors of A.

The power method for approximating eigenvalues is iterative. Assuming that the matrix A
has a dominant eigenvalue with corresponding dominant eigenvectors, we choose an initial
approximation of one of the dominant eigenvectors of A. This initial approximation must be a
nonzero vector in terms of only 0s and 1s . Finally, form the sequence given by

For large powers of k, and by properly scaling this


sequence, a good approximation of the dominant
eigenvector of A can be obtained.

PSEUDOCODE
1. Declare the variables : max ,dmax
2. Input the order of the symmetric matrix n
3. Declare the arrays : a[n][n],x[n],y[n],z[n]
4. Input the matrix elements:
for i=1 to n
j=1 to n
input aij
5. Input the initial guess eigen vector:
For i= 1 to n
Input xi
6. Loop do:
a. Multiply matrix a with the guess matrix x:
For i = 1 to n
xi=0
j = 1 to n
y i = y i + aij *xj
b. find out the largest eigen value and corresponding eigen vector:
max=fabs (y1)
for i=1 to n
j=1 to n
if fabs (yj)>max
max= yj
end for
z i= yi / max
end for
c. find the difference between consecutive eigen vectors:
for i= 1 to n
di=fabs(zi-xi)
d. assign zi to xi
e. find the maximum value dmax in di
for i=1 to n
if dmax>di
dmax= di
end loop while (dmax>0.0001)
7. print the dominant eigen value :max
8. print the dominant eigen vector : zi

CODING:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cout << "\nEnter the order of the symmetric matrix:";
cin >> n;
float a[n][n],x[n],y[n],max,z[n],d[20],dmax;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin >> a[i][j];
}
}
cout << "\nEnter the initial guess:";
for(int i=1;i<=n;i++)
cin >> x[i];
do
{
for(int i=1;i<=n;i++)
{
y[i]=0;
for(int j=1;j<=n;j++)
{
y[i]+=a[i][j]*x[j];
}
}
max=fabs(y[1]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(fabs(y[j])>max)
max=y[j];
}
z[i]=y[i]/max;
}
for(int i=1;i<=n;i++)
{
d[i]=fabs(z[i]-x[i]);
x[i]=z[i];
}
dmax=d[1];
for(int i=1;i<=n;i++)
{
if(dmax<d[i])
{
dmax=d[i];
}
} }while(dmax>0.0001);
cout << "\nEigen value:" << max;
cout << "\nEigen vector:";
for(int i=1;i<=n;i++)
cout << z[i];
}

OUTPUT:
The above written code generated the following outputs:
1. Enter the order of the symmetric matrix:3
2 -1 0
-1 2 -1
0 2 -1
Enter the initial guess: 1 0 0
Eigen value:
2.76932
Eigen vector:
1
-0.769298
-0.408199

2. Enter the order of the symmetric matrix:3


123
245
358
Enter the initial guess:1 0 1
Eigen value:
12.5136
Eigen vector:
0.377977
0.67604
1

3. Enter the order of the symmetric matrix:3


3 2 5
2 5 4
5 4 7
Enter the initial guess:1 1 1
Eigen value:
12.8861
Eigen vector:
0.641288
0.669881
1
DISCUSSION AND CONCLUSION:
Thus, from this lab session, we could simulate the power method for finding the eigen
vector and eigen value of a matrix based on our knowledge on computer programming.
Moreover, the theoretical concepts regarding the operation mechanism of those methods, the
way how it uses the technique of iterative matrix multiplication to find out the value of eigen
value was also clarified with the help of the above written code.

#2
CURVE FITTING : LINEAR AND EXPONENTIAL LAWS
THEORY:
INTRODUCTION
In case of statistical data analysis, in course of collecting the data during the observations, some
of the data may be either missing or erroneous at some points of observations. In such cases, it is
required to obtain an approximate value of the data at that point by inferring the values of nearby points
and constructing an approximate smooth curve joining them, that tentatively gives an idea of
whereabouts of the required data. The approximate smooth curve drawn as such, must be chosen such
that it provides the best fit for point to be derived, which is derived by curve fitting.
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit
to a series of data points, Curve fitting can involve either interpolation, where an exact fit to the data
is required, or smoothing in which a "smooth" function is constructed that approximately fits the data.
The main objectives of curve fitting are as follows:
1. to infer values of a function where no data are available
2. to summarize the relationships among two or more variables
3. as an aid for data visualization for scattered data
4. regression analysis of data.

There are various methods of curve fitting, among which the two of them aare discussed in
this lab session, viz linear curve fitting and exponential curve fitting.

LINEAR CURVE FITTING:

In linear curve fitting, the curve is assumed to be a straight line of the


form

y=f(x) = a + bx ------------- (1)

The goal is to identify the coefficients ‘a’ and ‘b’ such that f(x) fits
the data well into the linear curve.

For this the normal equation of equation (1) is developed as:

∑y =na +b∑x ___________________ (2)


∑xy= a∑x +b∑x2
Thus, by calculating all the sums of the variables required, the normal equations when solved
gives the value of a and b which defines the linear equation that best fits the points.
PSEUDOCODE:
1. Declare the variables: a,b,sumx=0,sumy=0,sumx2=0,sumxy=0, n
2. Input the number of readings to be taken
3. Input the data readings for xi an yi
4. Calculate the necessary sums for normal equation:
sumx= sumx+ x[i]
sumy= sumy+ y[i]
sumx2= sumx2+x[i]*x[i]
sumxy= sumxy + x[i]*y[i]
5. Calculate the values of a and b using cramer’s rule formulae:
a = (sumx*sumy-n*sumxy)/(sumx*sumx-n*sumx2)
b= (sumx*sumxy-sumx2*sumy)/(sumx*sumx-n*sumx2)
6. Display the fitting equation

CODING:
#include <iostream>
using namespace std;
int main()
{
int n;
float a,b,sumx=0,sumy=0,sumx2=0,sumxy=0;
cout<<"\n enter the no of readings:";
cin>>n;
float x[n],y[n];
cout<<"enter data for x and y:";
for (int i=1;i<=n;i++)
{
cin>>x[i]>>y[i];
sumx+=x[i];
sumy+=y[i];
sumx2+=(x[i]*x[i]);
sumxy+=(x[i]*y[i]);
}
a=(sumx*sumy-n*sumxy)/(sumx*sumx-n*sumx2);
b=(sumx*sumxy-sumx2*sumy)/(sumx*sumx-n*sumx2);
cout<<"\n the best fitting equation is:\n y="<<a<<"x+"<<b;
}
Liinear curve fitting
OUTPUT :
10
1. enter the no of readings:4 9
enter data for x and y: 8
2 4 7
5 3 6
7 3 5
1 9 4
the best fitting equation is: 3
y=-0.802198x+7.75824 2 y = -0.8022x + 7.7582
1
0
0 2 4 6 8

2. enter the no of readings:5


35
enter data for x and y:
22 11 30 y = 0.4176x + 3.5566
44 20
25
55 23
66 32 20
41 27
15

the best fitting equation is: 10


y=0.417619x+3.55658 5

0
0 20 40 60 80

3. enter the no of readings:8


enter data for x and y:
10
2 5 y = 0.2361x + 4.46
2 6 9
7 8 8
8 3 7
1 5 6
7 9
5
3 2
1 5 4
3
the best fitting equation is: 2
y=0.23614x+4.45996
1
0
0 2 4 6 8 10
EXPONENTIAL CURVE FITTING:
Like linear curve fitting, in exponential curve fitting, the curve is assumed to be an
exponential curve of the form

y=f(x) = a e bx ------------- (1)


on taking log on both sides
or, log y = log a +bx
or, Y= A + bx
where Y=log y
A=log a
Thus, by making the exponential curve into a linear we can easily carry out the curve fitting
analysis for an exponential curve.
The goal is to identify the coefficients ‘a’ and ‘b’ such that f(x) fits the data well into the
linear curve.

For this the normal equation of equation (1) is developed as:

∑Y =nA +b∑x ___________________ (2)


∑xY= A∑x +b∑x2
Thus the required coefficients a and b are derived as a=antilog(A)
Thus, by calculating all the sums of the variables required, the normal equations when solved
gives the value of a and b which defines the linear equation that best fits the points.

PSEUDOCODE:
1. Declare the variables: A, a,b,sumx=0,sumy=0,sumx2=0,sumxy=0, n
2. Input the number of readings to be taken
3. Input the data readings for xi an yi
4. Calculate the necessary sums for normal equation:
sumx= sumx+ x[i]
sumy= sumy+ log(y[i])
sumx2= sumx2+x[i]*x[i]
sumxy= sumxy + x[i]*log(y[i])
5. Calculate the values of a and b using cramer’s rule formulae:
A = (sumx2*sumy-sumxy*sumx)/(n*sumx2-sumx*sumx)
b= (n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx)
a=exp(A)
6. Display the fitting equation
CODING:

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
float A,a,b,sumx=0,sumy=0,sumx2=0,sumxy=0;
cout<<"\n enter the no of readings:";
cin>>n;
float x[n],y[n];
cout<<"enter data for x and y:";
for (int i=1;i<=n;i++)
{
cin>>x[i]>>y[i];
sumx+=x[i];
sumy+=log(y[i]);
sumx2+=(x[i]*x[i]);
sumxy+=(x[i]*log(y[i]));
}
A=(sumx2*sumy-sumxy*sumx)/(n*sumx2-sumx*sumx);
b=(n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx);
a=exp(A);
cout<<"\n the best fitting equation is:\n Y= "<<a<<" e^ "<<b;
}

OUTPUT :

1. enter the no of readings:3 7


enter data for x and y: y = 1.5943e0.2747x
6
1 2
3 4 5

5 6 4

3
the best fitting equation is:
Y= 1.59431 e^ 0.274653 2

0
0 2 4 6
30
2. enter the no of readings:5
enter data for x and y: 25
2 4 y = 4.0588e0.0906x
7 8 20
9 11
15
16 22
20 19 10

the best fitting equation is: 5


Y= 4.0588 e^ 0.0906421
0
0 5 10 15 20 25

100
3. enter the no of readings:5 y = 6.033e0.0914x
enter data for x and y: 80
22 45
60
26 68
28 70 40
29 90
30 95 20

the best fitting equation is: 0


0 10 20 30 40
Y= 6.03288 e^ 0.0914239

DISCUSSION AND CONCLUSION :


Thus, from this lab session, we could simulate the method of curve fitting for finding the best
fitting equation for a given set of scattered data based on our knowledge on computer programming.
Moreover, the graphical concepts regarding the operation mechanism of those methods was also
clarified with the help of the above written code.

You might also like