Question 1

\[A = \begin{pmatrix} 1 & -2 & 3 & 0 \\ 5 & 0 & 6 & 0 \\ -1 & 2 & 0 & 3 \\ 4 & 1 & -2 & 0 \end{pmatrix}\]

Consider the matrix \(A\) above. Please find all the parts listed below.

First, we will define matrix \(A\) in R.

A <- matrix(data = c(1,-2,3,0,5,0,6,0,-1,2,0,3,4,1,-2,0),nrow = 4,ncol = 4,byrow = T)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1   -2    3    0
## [2,]    5    0    6    0
## [3,]   -1    2    0    3
## [4,]    4    1   -2    0
  1. Determinant
det(A)
## [1] 177
  1. Transpose
t(A)
##      [,1] [,2] [,3] [,4]
## [1,]    1    5   -1    4
## [2,]   -2    0    2    1
## [3,]    3    6    0   -2
## [4,]    0    0    3    0
  1. Trace
# install.packages("matrixcalc")
library(matrixcalc)
## Warning: package 'matrixcalc' was built under R version 4.5.2
matrix.trace(A)
## [1] 1
  1. Rank
# install.packages("matrixcalc")
library(matrixcalc)
matrix.rank(A)
## [1] 4
  1. Inverse
# install.packages("matrixcalc")
library(matrixcalc)
matrix.inverse(A)
##             [,1]        [,2]      [,3]       [,4]
## [1,]  0.10169492  0.01694915 0.0000000  0.2033898
## [2,] -0.57627119  0.23728814 0.0000000 -0.1525424
## [3,] -0.08474576  0.15254237 0.0000000 -0.1694915
## [4,]  0.41807910 -0.15254237 0.3333333  0.1694915
  1. Reduced-row echelon form
# install.packages("pracma")
library(pracma)
rref(A)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
  1. Cofactors
# install.packages("matlib")
library(matlib)
## 
## Attaching package: 'matlib'
## The following objects are masked from 'package:pracma':
## 
##     angle, inv
## The following object is masked from 'package:matrixcalc':
## 
##     vec
for (i in 1:nrow(A)) {
  for (j in 1:ncol(A)) {
    cat("Cofactor at entry (",i,",",j,") is",cofactor(A,i,j),"\n")
  }
}
## Cofactor at entry ( 1 , 1 ) is 18 
## Cofactor at entry ( 1 , 2 ) is -102 
## Cofactor at entry ( 1 , 3 ) is -15 
## Cofactor at entry ( 1 , 4 ) is 74 
## Cofactor at entry ( 2 , 1 ) is 3 
## Cofactor at entry ( 2 , 2 ) is 42 
## Cofactor at entry ( 2 , 3 ) is 27 
## Cofactor at entry ( 2 , 4 ) is -27 
## Cofactor at entry ( 3 , 1 ) is 0 
## Cofactor at entry ( 3 , 2 ) is 0 
## Cofactor at entry ( 3 , 3 ) is 0 
## Cofactor at entry ( 3 , 4 ) is 59 
## Cofactor at entry ( 4 , 1 ) is 36 
## Cofactor at entry ( 4 , 2 ) is -27 
## Cofactor at entry ( 4 , 3 ) is -30 
## Cofactor at entry ( 4 , 4 ) is 30
  1. Minors
# install.packages("matlib")
library(matlib)
for (x in 1:nrow(A)) {
  for (y in 1:ncol(A)) {
    cat("Minor at entry (",x,",",y,") is",minor(A,x,y),"\n")
  }
}
## Minor at entry ( 1 , 1 ) is 18 
## Minor at entry ( 1 , 2 ) is 102 
## Minor at entry ( 1 , 3 ) is -15 
## Minor at entry ( 1 , 4 ) is -74 
## Minor at entry ( 2 , 1 ) is -3 
## Minor at entry ( 2 , 2 ) is 42 
## Minor at entry ( 2 , 3 ) is -27 
## Minor at entry ( 2 , 4 ) is -27 
## Minor at entry ( 3 , 1 ) is 0 
## Minor at entry ( 3 , 2 ) is 0 
## Minor at entry ( 3 , 3 ) is 0 
## Minor at entry ( 3 , 4 ) is -59 
## Minor at entry ( 4 , 1 ) is -36 
## Minor at entry ( 4 , 2 ) is -27 
## Minor at entry ( 4 , 3 ) is 30 
## Minor at entry ( 4 , 4 ) is 30
  1. Eigenvalues
eigenvalues <- eigen(A)$values
for (a in seq_along(eigenvalues)) {
  cat("Eigenvalue",a,":",eigenvalues[a],"\n")
}
## Eigenvalue 1 : -2.195689+4.260136i 
## Eigenvalue 2 : -2.195689-4.260136i 
## Eigenvalue 3 : 2.695689+0.6625915i 
## Eigenvalue 4 : 2.695689-0.6625915i
  1. Eigenvectors
eigenvectors <- eigen(A)$vectors
for (e in 1:ncol(eigenvectors)) {
  cat("Eigenvector",e,":\n")
  print(eigenvectors[, e])
  cat("\n")
}
## Eigenvector 1 :
## [1] -0.34213048+0.2782769i -0.04116664+0.5046828i -0.05816267-0.4458144i
## [4]  0.58904664+0.0000000i
## 
## Eigenvector 2 :
## [1] -0.34213048-0.2782769i -0.04116664-0.5046828i -0.05816267+0.4458144i
## [4]  0.58904664+0.0000000i
## 
## Eigenvector 3 :
## [1]  0.1167347-0.08451662i -0.8368898+0.00000000i -0.4732780-0.02198883i
## [4]  0.1764246-0.15246055i
## 
## Eigenvector 4 :
## [1]  0.1167347+0.08451662i -0.8368898+0.00000000i -0.4732780+0.02198883i
## [4]  0.1764246+0.15246055i

Question 2

Put \([729.03,903.27,790.23,97.32]\) in order from least to greatest.

# install.packages("tidyverse")
library(tidyverse)
## Warning: package 'lubridate' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ purrr::cross()  masks pracma::cross()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
q2_data <- data.frame(Value = c(729.03,903.27,790.23,97.32))
q2_data %>%
  arrange(Value) # arrange(): ascending order
##    Value
## 1  97.32
## 2 729.03
## 3 790.23
## 4 903.27

Question 3

Solve the system of equations below.

\[5x - 3y = 27 \\ 2x + 8y = -3\]

q3_data <- data.frame(X = c(5,2),
                      Y = c(-3,8),
                      Constants = c(27,-3))
q3_model <- lm(Constants ~ . - 1,data = q3_data)
coef(q3_model)
##    X    Y 
##  4.5 -1.5

Question 4

Differentiate \(f(x) = \sin(\cos(x))\).

# install.packages("Deriv")
library(Deriv)
f <- function(x) {
  sin(cos(x))
}
f_prime <- Deriv(f)
f_prime
## function (x) 
## -(cos(cos(x)) * sin(x))