Search code examples
rmatrixlinear-algebraeigenvalueeigenvector

How can I make a symmetric tridiagonal matrix (Wilkinson matrix) in R and compute the eigenvalues?


In linear algebra, Wilkinson matrices are symmetric, tridiagonal, order-N matrices with pairs of nearly, but not exactly, equal eigenvalues. Wilkinson matrices have applications in many fields, including scientific computing, numerical linear algebra, and signal processing.(Wikipedia)

Following the matrix example from wikipedia, I have this matrix:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3    1    0    0    0    0    0
[2,]    1    2    1    0    0    0    0
[3,]    0    1    1    1    0    0    0
[4,]    0    0    1    0    1    0    0
[5,]    0    0    0    1    1    1    0
[6,]    0    0    0    0    1    2    1
[7,]    0    0    0    0    0    1    3

How can I create this matrix and compute the eigenvalues in R?


Solution

  • With this script you can create a Wilkinson matrix and compute the eigen values

    identity_6_by_7 <- diag(rep.int(1, 6), 6, 7) # create a 6x7 matrix with ones on the main diagonal
    below_the_diagonal <- rbind(0, identity_6_by_7) # create a row of zeros below the diagonal 
    identity_7_by_6 <- diag(rep.int(1, 6), 7, 6) # matrix with the ones offset one up from the diagonal
    above_the_diagonal <- cbind(0, identity_7_by_6) # create a row of zeros above the diagonal 
    on_the_diagonal <- diag(abs(seq.int(-3, 3))) # diagonal of values from abs(-3 to 3)
    wilkinson_21 <- below_the_diagonal + above_the_diagonal + on_the_diagonal
    eigen(wilkinson_21)$values # eigen values
    

    You can check the eigen:

    eigen(wilkinson_21)$values
    [1]  3.7615572  3.7320508  2.3633282  2.0000000  1.0000000  0.2679492 -1.1248854