This file can be seen fully rendered on RPubs here.
u= c(0.5,0.5)
v=c(3,-4)
dp_uv<- u%*%v # %*% = matrix multiplication. If both vectors same length inner-product is returned
dp_uv
## [,1]
## [1,] -0.5
# Euclidean norm of the vector = the square root of the inner-product of a vector with itself.
dp_uu <- u%*%u
dp_vv <- v%*%v
Length of u:
l_u <- sqrt(dp_uu)
l_u
## [,1]
## [1,] 0.7071068
Length of v:
l_v <- sqrt(dp_vv)
l_v
## [,1]
## [1,] 5
(3*u)-(2*v)
## [1] -4.5 9.5
#The dot-product between two vectors is proportional to their lengths and angle between them.
#dp_uv = cosine of the angle between u and v
theta <- acos(dp_uv/(l_u*l_v)) # acos() = arc-cosine
theta # given in radians
## [,1]
## [1,] 1.712693
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. Please note that you do have to worry about zero pivots, and that you should not use the built-in function solve to solve this system or use matrix inverses.The approach that you should employ is to construct an Upper Triangular Matrix and then back-substitute to get the solution. Alternatively, you can augment the matrix A with vector b and jointly apply the Gauss Jordan elimination procedure.
Please test it with the system below and it should produce a solution \(x = [-1.55;-0.32;0.95]\) \[\begin{bmatrix}1 & 1 & 3 \\2 & -1 & 5 \\-1 & -2 & 4\end{bmatrix}\,\begin{bmatrix}x_1 \\x_2 \\x_3\end{bmatrix}=\begin{bmatrix}1 \\2 \\6\end{bmatrix}\]
The below function will only work for 3-equation systems
#inputs to sys_solver function are matrix A & constraint vector b
A <- array(c(1, 2, -1, 1, -1, -2, 3, 5, 4), dim=c(3,3))
A # Make sure it looks like our problem prompt
## [,1] [,2] [,3]
## [1,] 1 1 3
## [2,] 2 -1 5
## [3,] -1 -2 4
b <- c(1, 2, 6) # constraint vector
sys_solver <- function(A, b) {
# create a vector to store outputs.
x = numeric(3) # size of the vector
# bind A and b to create a new matrix (augmented matrix)
new_m = cbind(A, b)
# need 1 in top left pivot
if (new_m[1,1] == 0) {
# need to switch with row that has non-zero in first column
if (new_m[2,2] != 0) {
# switch rows 1 and 2 as row 2 has a non-zero in column 1
new_m = new_m[c(2,1,3),]
} else {
# switch rows 3 and 1 since row 3 has a non-zero
new_m = new_m[c(3,2,1),]
}
} else if (new_m[1,1] != 1) {
# else divide 1st row by row 1 column 1 to get 1 in pivot
new_m[1,] <- new_m[1,] / new_m[1,1]
}
# Zero other entries in column 1
if (new_m[2,1] !=0) {
# use value of row 2 column 1 for vector addition to zero the row
vec <- new_m[2,1] * new_m[1,]
# reduce row 2 using the new vec object
new_m[2,] <- -new_m[2,] + vec
}
if (new_m[3,1] !=0) {
# use value of row 3 column 1 for vector addition to zero second row
vec <- new_m[3,1] * new_m[1,]
# now reduce row 2 using the vec
new_m[3,] <- -new_m[3,] + vec
}
# check if row 2 column 2 are 0. If no, switch for row 3 to get non-zero
if (new_m[2,2] == 0) {
# switch rows 2 and 3 as row 2 has 0 in column 2
new_m = new_m[c(1,3,2),]
}
# divide second row by row 2 column 2 to get 1 in pivot
new_m[2,] <- new_m[2,] / new_m[2,2]
# use value of row 3 column 2 for vector addition to zero row 3 column 2
vec <- new_m[3,2] * new_m[2,]
# now reduce row 3 using vec
new_m[3,] <- -new_m[3,] + vec
# solve for x with back subsitution
x[3] <- new_m[3,4] / new_m[3,3]
x[2] <- (new_m[2,4] - new_m[2,3]*x[3]) / new_m[2,2]
x[1] <- (new_m[1,4] - new_m[1,2]*x[2] - new_m[1,3]*x[3]) / new_m[1,1]
return(x)
}
# Try it out!
sys_solver(A,b)
## [1] -1.5454545 -0.3181818 0.9545455
Check our work using the built in solve function:
A <- array(c(1, 2, -1, 1, -1, -2, 3, 5, 4), dim=c(3,3))
b <- c(1, 2, 6)
solve(A, b)
## [1] -1.5454545 -0.3181818 0.9545455