Algorithms and Subroutines for Solving Differential Equations
Routine Name: powerIteration
Author: Philip Nelson
Language: C++
powerIteration
calculates the largest Eigenvalue of a N
xN
matrix.
powerIteration(Matrix<T, N, N> const& A, unsigned int const& MAX)
requires:
Matrix<T, N, N> const& A
- an N
xN
matrixunsigned int const& MAX
- the maximum number of iterationsThe largest Eigenvalue
template <typename T, std::size_t N>
T powerIteration(Matrix<T, N, N> const& A, unsigned int const& MAX)
{
std::array<T, N> b_k;
for (auto&& e : b_k)
e = randDouble(0.0, 10.0);
for (auto i = 0u; i < MAX; ++i)
{
auto Ab_k = A * b_k;
auto norm = pNorm(Ab_k, 2);
b_k = Ab_k / norm;
}
return pNorm(A * b_k, 2);
}
int main()
{
Matrix<double, 5, 5> A(
[](unsigned int const& i, unsigned int const& j) { return 1.0 / (i + j + 1.0); });
auto eigval = powerIteration(A, 1000u);
std::cout << "A\n" << A << std::endl;
std::cout << "Largest Eigenvalue\n" << eigval << std::endl;
}
A
| 1 0.5 0.333 0.25 0.2 |
| 0.5 0.333 0.25 0.2 0.167 |
| 0.333 0.25 0.2 0.167 0.143 |
| 0.25 0.2 0.167 0.143 0.125 |
| 0.2 0.167 0.143 0.125 0.111 |
Largest Eigenvalue
1.57
Last Modification date: 27 February 2018