# eigen.R Linear algebra: eigenvalue and eigenvector calculations
rm(list=ls()) #equivalent of Maple restart command
# a matrix is entered by giving the number of rows, number of columns, and a list of entries,
# if you say byrow=T, then the entries are left to right, top row to bottom row.
A<-matrix(nrow=2,ncol=2,byrow=T,c(-4,5/2,-5/2,1))
#It is easy to get the eigenvalues, but watch out, they could be complex numbers
eigen(A)
#Note that the result is a list, with 2 parts.
# A$values has the eigenvalues, and A$vectors has the eigenvectors
V<-eigen(A)$vectors
#To select a particular eigenvector, use the select operator []
# V[1,] selects the first eigenvector
A%*%V[1,]
eigen(A)$values[1]*V[1,]
#Transformation procedure
transform<-function(A,seg,ADD=F)
{
if(ADD){par(new=T)}
plot(seg,type="l",xlim=c(-10,10),ylim=c(-10,10),lwd=3,asp=1)
new<-matrix(ncol=2,nrow=nrow(seg))
for (ii in 1:nrow(seg))
{new[ii,]<-A%*%seg[ii,]}
lines(new,col="red",lwd=1)
abline(v=0);abline(h=0)
par(new=F)
}
seg<-matrix(nrow=2,ncol=2,byrow=T,c(0,0,1,1))
transform(A,seg)
#Here's another example, this time with two distinct complex eigenvalues.
#Oftentimes we don't care about specific complex eigenvalues;
#the mere absence of real eigenvalues is all we need to know.
#Other times we only care about features of the complex eigenvalues,
#such as whether they are pure imaginary,
#or if they have positive or negative real parts.
B<- matrix(nrow=2, ncol=2, c(4, 1, -2, 3),byrow=T); eigen(B);
#The next one has two distinct pure imaginary eigenvalues.
C<- matrix(nrow=2, ncol=2, c(-0.4, -4, 2, 0.4),byrow=T); eigen(C);
#The next has two distict real eigenvalues
DD<- matrix(nrow=2, ncol=2, c(2, 3, 4, 1),byrow=T); eigen(DD);
V<- eigen(DD)$vectors;
#Let's see if we can extract those two independent eigenvectors
#The first eigenvector is the first column of the matrix V
v1<-V[,1]; v1
v2<-V[,2]; v2
#to scale the vectors to have 1 as the first element
v1<-v1/v1[1] ;v1
v2<-v2/v2[1] ;v2
#How about that?
#Fortunately, unless we are planning to write down explicit solutions
#of our differential equations, we seldom need all this.
#And since the solutions corresponding to the various eigenvectors are solutions
#of the linearized system, which we are only using as a guide to the behaviour
#of the original non-linear systems in the vicinity of equilibrium points,
#there isn't a lot of point in getting formulas. All we are really interested in
#is the stability properties and local behaviour of the linearized system
#(which is supposed to reflect that of the original system), and,
#as mentioned above, this is often possible to read just from the kinds of eigenvalues
#that we get.