Eigen 2
Eigen 2
Eigen 2
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
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
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.
The goal is to identify the coefficients ‘a’ and ‘b’ such that f(x) fits
the data well into the linear curve.
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
0
0 20 40 60 80
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 :
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
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