I generated 1000 2x2 matrices whose elements are random numbers that range between -10 and 10.
I provided the codes I have so far.
But I'm not sure if this is the right code to find whether my list of eigenvalues are complex or not. Then for each matrix, I have to determine if the system is a stable node (both eigenvalues are real and negative); an unstable node (both eigenvalues are real and positive); a saddle (both eigenvalues are real one is positive the other negative); a stable focus (complex eigenvalues with a negative real part); an unstable focus (complex eigenvalues with a positive real part); or a center (imaginary eigenvalues, real part is zero).
I also have counters set up but not sure how to incorporate them. When I enter in the code, nothing shows up.
M=lapply(1:1000, function(z) matrix(runif(1000,min=-10,max=10), ncol = 2, nrow = 2))
eig=lapply(M, eigen)
V=sapply(eig, `[[`, "values")
SFcounter=0
if (is.complex(V)==T)
Re(V)>0
SFcounter=SFcounter+1
For each node
you have create a column in V
. you can use the Im
function to extract the imaginary part (whether its equal to zero or otherwise) of any complex number. You can extract the real component with Re
. Further, if you're interested in classifying real numbers, it is all those which satisfy Im(V) == 0
[the imaginary component is equal to zero].
If you use apply
you can assess each eigenvalue pair in V
, which are grouped together at each column. Based on your different classification criteria, you can identify these points using if
statements:
node.classification <- function(x) {
if ( (Im(x) == 0) && (Re(x) < 0) ) { #both eigenvalues are real and negative
return("stable")
} else {
if ( (Im(x) == 0) && (Re(x) > 0) ) { #both eigenvalues are real and positive
return("unstable")
} else {
if ( (Im(x) == 0) && sum((Re(x) > 0) == 1) ){ #both real one pos./one neg.
return("saddle")
} else {
if ( (Im(x) != 0) && (Re(x) < 0) ) { #each complex, real part negative
return("stable focus")
} else {
if ( (Im(x) != 0) && (Re(x) > 0) ) { #each complex, real part positive
return("unstable focus")
} else {
return(NA)
}
}
}
}
}
}
head(apply(V, 2, node.classification))
[1] "unstable" "stable focus" "stable"
[4] "unstable" "unstable" "unstable focus"