# Thread: Determination of eigenvalues and their condition number with C

1. ## Determination of eigenvalues and their condition number with C

Hello,

I'm pretty new to the forum.
I have the following problem:

I would like to calculate eigenvalues with a C program code. Now I suspect that the eigenvalue problem is wrongly conditioned. For this reason I would like to calculate the condition number. does anyone know how to program this?

Therefore I have inserted a part of the program here:

Code:
double c,f,g,r,s;
int i,j,k,l,done;
/* search for rows isolating an eigenvalue and push them down */
for (k = n - 1; k >= 0; k--) {
for (j = k; j >= 0; j--) {
for (i = 0; i <= k; i++) {
if (i != j && fabs(mat[pos(j,i,n)]) != 0) break;
}

if (i > k) {
scale[k] = j;

if (j != k) {
for (i = 0; i <= k; i++) {
c = mat[pos(i,j,n)];
mat[pos(i,j,n)] = mat[pos(i,k,n)];
mat[pos(i,k,n)] = c;
}

for (i = 0; i < n; i++) {
c = mat[pos(j,i,n)];
mat[pos(j,i,n)] = mat[pos(k,i,n)];
mat[pos(k,i,n)] = c;
}
}
break;
}
}
if (j < 0) break;
}
I would be very grateful for your help!

Best regards

2. Welcome! Do you know how to compute that on paper? If so, how would you do it, step by step? If not, then you should be looking for a maths forum first.

So the condition indicates how well a problem is solved with an algorithm on a computer. Since we calculate eigenvalues it would be useful to look at the respective matrices. Let me give you an example:

Calculation of the condition of the matrix

Code:
A:=\begin{pmatrix}
-2 & 1 & 0 \\
1 & -2 & 1\\
0 & 1 & -2
\end{pmatrix}

The eigenvalues are
Code:
\lambda _{1} = -2, \lambda _{2} = -2+\sqrt{2} and \lambda _{3}= -2-\sqrt{2}
.

This makes the condition
Code:
\kappa(A) = \left | \frac{\lambda _{max}}{\lambda _{min}} \right | = \left | \frac{-2-\sqrt{2}}{-2+\sqrt{2}} \right | = 5.8
.

The system of equations or the matrix is thus well conditioned.

I hope you can read the latex code. Is there any way to convert this into a program code? Or is it useful to look at the matrix here? Are there any other ways how I can check if the program delivers good results?

4. Thanks for the book tip.
Here are some examples of how to calculate eigenvalues. Like e.g. with the QR-method or with intererations. But it is not given how to test the efficiency of these methods. So how well they can solve the eigenvalue problem. Do you know any other sources?