u <- matrix(c(0.5, 0.5), nrow = 2)
v <- matrix(c(3, -4), nrow = 2)
cp <- crossprod(u, v)
print (cp)
## [,1]
## [1,] -0.5
length_u <- sqrt(sum(u * u))
length_v <- sqrt(sum(v * v))
print(length_u)
## [1] 0.7071068
print(length_v)
## [1] 5
lc <- 3*u - 2*v
print (lc)
## [,1]
## [1,] -4.5
## [2,] 9.5
\[ \cos \theta = \frac{u \cdot v}{||u|| \ ||v||} \]
theta <- acos( crossprod(u, v) / ( length_u * length_v ) )
print (theta)
## [,1]
## [1,] 1.712693
# Function to determine the solution of System of Linear Equations
eliminateAndSolve <- function(A, b) {
# Step 1: Construct an Augmented Matrix M by applying cbind on both Matrix A and Vector b
M = cbind(A, b)
# Step 2: Use Gauss Jordan Elimination method on the Augmented matrix M
for (i in 1:nrow(M)) {
for (j in 1:ncol(M)) {
if (i == j) {
for (k in 1:nrow(M)) {
if (M[k,j] != 0 & k != i) {
multiplier <- M[i,j] / M[k,j]
M[k,] <- M[k,] * multiplier - M[i,]
}
}
}
}
}
# Step 3: Loop thru the Augmented matrix to create an identity matrix by dividing each row with either M[1, 1] or M[2, 2] or M[3, 3]
for (i in 1:nrow(M)) {
M[i,] <- M[i,] * (1/M[i,i])
}
# Step 4: Obtain values for variables
x1 <- M[1, 4]
x2 <- M[2, 4]
x3 <- M[3, 4]
X <- matrix(c(x1, x2, x3), nrow = 3)
}
equation <- matrix(c(1, 1, 3, 2, -1, 5, -1, -2, 4), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(1, 2, 6), nrow = 3)
# Display the equation Matrix
print (equation)
## [,1] [,2] [,3]
## [1,] 1 1 3
## [2,] 2 -1 5
## [3,] -1 -2 4
# Display the constraint vector
print (constraints)
## [,1]
## [1,] 1
## [2,] 2
## [3,] 6
solution = round (eliminateAndSolve(equation, constraints), 2)
print (solution)
## [,1]
## [1,] -1.55
## [2,] -0.32
## [3,] 0.95
equation <- matrix(c(-1, -5, -5, 4, -5, 4, 1, 5, -1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(2, 19, -20), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] -2
## [2,] -3
## [3,] 3
equation <- matrix(c(-4, -5, -1, -2, -5, -2, -2, 5, 2), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(18, 12, 4), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] -4
## [2,] 0
## [3,] -2
equation <- matrix(c(-1, -5, 1, -5, -5, 5, 2, 5, -3), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(17, 5, -10), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] -1
## [2,] -4
## [3,] -4
equation <- matrix(c(4, 4, 1, 2, -4, 1, 5, -4, -5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(24, 0, 12), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 4
## [2,] 2
## [3,] 0
equation <- matrix(c(4, -4, 4, 4, 1, -2, -3, -3, -4), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-4, 5, -16), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 1
## [2,] 3
## [3,] 1
equation <- matrix(c(1, -6, 4, 1, 1, -4, 2, 2, 5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-12, 12, -15), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 0
## [2,] 0
## [3,] -3
equation <- matrix(c(1, -1, -2, 3, 2, 0, -4, 1, -1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-6, -25, 12), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] -5
## [2,] -5
## [3,] 3
equation <- matrix(c(5, 5, 5, 4, 3, 3, -4, 3, 3), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-20, -6, 9), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 6
## [2,] Inf
## [3,] -Inf
equation <- matrix(c(-6, 5, 2, -2, 1, 4, 4, -5, 5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-11, -9, -4), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 4
## [2,] 3
## [3,] -1
equation <- matrix(c(-6, -2, 2, 3, -2, -4, 6, -2, -6), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-8, 8, -18), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] Inf
## [2,] -Inf
## [3,] Inf
equation <- matrix(c(5, -4, 2, -1, -5, 6, -1, -4, 5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(21, -24, -21), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 5
## [2,] -1
## [3,] -4
equation <- matrix(c(6, -1, 3, 5, 5, -5, 3, -1, 4), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-9, 20, -5), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] -1
## [2,] 6
## [3,] 1
equation <- matrix(c(-3, -1, -3, -5, 3, 6, -6, -4, 1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-8, -4, -20), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 2
## [2,] 2
## [3,] 0
equation <- matrix(c(-5, 3, 6, -3, 1, 5, -4, 2, 1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(4, -5, 13), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] -2
## [2,] 4
## [3,] -3
equation <- matrix(c(3, -3, 4, 1, 2, -3, 4, -1, 1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-23, 25, 25), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] Inf
## [2,] Inf
## [3,] Inf
equation <- matrix(c(-6, -2, -1, 5, 1, -6, -4, -6, -6), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-17, 19, -20), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
## [,1]
## [1,] 2
## [2,] 3
## [3,] -1