library(Matrix)
library(ggplot2)
A <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
eigen_result <- eigen(A)
diagonal_matrix <- diag(eigen_result$values)
eigenvector_df <- data.frame(x = c(0, eigen_result$vectors[1, 1]),
y = c(0, eigen_result$vectors[2, 1]))
eigenvector_plot <- ggplot(eigenvector_df, aes(x = x, y = y)) +
geom_segment(aes(xend = x, yend = y), arrow = arrow(length = unit(0.3, "cm"))) +
xlim(-1, 1) + ylim(-1, 1) +
theme_minimal()
diagonalization_df <- data.frame(x = c(0, eigen_result$vectors[1, 1], 0, eigen_result$vectors[1, 2]),
y = c(0, eigen_result$vectors[2, 1], 0, eigen_result$vectors[2, 2]))
diagonalization_plot <- ggplot(diagonalization_df, aes(x = x, y = y)) +
geom_segment(aes(xend = x, yend = y), arrow = arrow(length = unit(0.3, "cm"))) +
xlim(-1, 1) + ylim(-1, 1) +
theme_minimal()
print("Eigenvalues:")
## [1] "Eigenvalues:"
print(eigen_result$values)
## [1] 5.3722813 -0.3722813
print("Eigenvectors:")
## [1] "Eigenvectors:"
print(eigen_result$vectors)
## [,1] [,2]
## [1,] -0.5657675 -0.9093767
## [2,] -0.8245648 0.4159736
print("Diagonal Matrix:")
## [1] "Diagonal Matrix:"
print(diagonal_matrix)
## [,1] [,2]
## [1,] 5.372281 0.0000000
## [2,] 0.000000 -0.3722813
print("Plot Eigenvectors:")
## [1] "Plot Eigenvectors:"
print(eigenvector_plot)

print("Plot Diagonalization:")
## [1] "Plot Diagonalization:"
print(diagonalization_plot)
