(1) Calculate the dot product u:v where u = [0.5; 0.5] and v = [3;4]
## [1] 3.5
Could also have used:
* t(u) %% v
crossprod(u,v)
(2) What are the lengths of u and v? Please note that the mathematical notion of the length of a vector is not the same as a computer science definition.
## [1] 0.7071068
Could also have used:
* norm(u, type=“2”)
## [1] 5
Could also have used:
* norm(v, type=“2”)
(3) What is the linear combination: 3u + 2v?
## [1] 7.5 9.5
(4) What is the angle between u and v?
Making use of the formula:
rad <- acos(dot.uv / (norm.u * norm.v))
print(paste0("The angle between the vectors is: ", round(rad * 180/pi,3), " degrees"))
## [1] "The angle between the vectors is: 8.13 degrees"
Set up a system of equations with 3 variables and 3 constraints and solve for x. Please write a function in R that will take two variables (matrix A & constraint vector b) and solve using elimination. Your function should produce the right answer for the system of equations for any 3-variable, 3-equation system. You don’t have to worry about degenerate cases and can safely assume that the function will only be tested with a system of equations that has a solution.
…The approach that you should employ is to construct an Upper Triangular Matrix and then back-substitute to get the solution…
First, I create a function to decompose the A matrix, verifying at the end that AX = B.
A <- NA # initialize matrices
U <- NA
L <- NA
decomp <- function(mat){
A <- matrix(mat, nrow = 3, byrow = T) # construct matrices from 9-dim vector
print("Matrix A")
print(A)
print("")
U <- A
a1 <- U[2,1]/U[1,1] # decompose Upper Matrix
U[2,] <- U[1,] * -(a1) + U[2,]
a2 <- U[3,1]/U[1,1]
U[3,] <- U[1,] * -(a2) + U[3,]
a3 <- U[3,2]/U[2,2]
U[3,] <- U[2,] * -(a3) + U[3,]
print("Matrix U")
print(U)
print("")
L <- matrix(0, nrow = 3, ncol = 3, byrow = T) # initialize Lower Matrix
L[1,1] <- 1; L[2,2] <- 1; L[3,3] <- 1
L[2,1] <- a1 # decompose Lower Matrix
L[3,1] <- a2
L[3,2] <- a3
print("Matrix L")
print(L)
print("")
LU <- L %*% U ## check that A = LU
print("Matrix Decomposition LU")
print(LU)
print("")
print(paste0("The original matrix and multiplication of decomposed LU matrices is the same: ", identical(A,round(LU,4))))
U <<- U ## save as global variables to retain for solve function
L <<- L
}
Next, I create a second function to imput the constraint vector.
## Recall AX = B, since A = LU
## LUX = B
## solve LY = B for Y, then
## UX = Y for X
Y <- NA # initialize factors
X <- NA
solve.eq <- function(constraint){
B <- constraint
Y[1] <- B[1] # perform backward substitution
Y[2] <- B[2] - Y[1] * L[2,1]
Y[3] <- B[3] - Y[1] * L[3,1] - Y[2] * L[3,2]
X[3] <- Y[3] / U[3,3]
X[2] <- (Y[2] - X[3] * U[2,3])/U[2,2]
X[1] <- (Y[1] - X[2] * U[1,2] - X[3] * U[1,3])/U[1,1]
print(round(X,2))
}
Finally, I solve with the test matrices provided.
## [1] "Matrix A"
## [,1] [,2] [,3]
## [1,] 1 1 3
## [2,] 2 -1 5
## [3,] -1 -2 4
## [1] ""
## [1] "Matrix U"
## [,1] [,2] [,3]
## [1,] 1 1 3.000000
## [2,] 0 -3 -1.000000
## [3,] 0 0 7.333333
## [1] ""
## [1] "Matrix L"
## [,1] [,2] [,3]
## [1,] 1 0.0000000 0
## [2,] 2 1.0000000 0
## [3,] -1 0.3333333 1
## [1] ""
## [1] "Matrix Decomposition LU"
## [,1] [,2] [,3]
## [1,] 1 1 3
## [2,] 2 -1 5
## [3,] -1 -2 4
## [1] ""
## [1] "The original matrix and multiplication of decomposed LU matrices is the same: TRUE"
## [1] -1.55 -0.32 0.95
The result is confirmed.