u <- c(0.5,0.5)
v <- c(3,-4)
dot_prod <- u %*% v
dot_prod
## [,1]
## [1,] -0.5
Length of a vector We’ll define the length of vector (or the Euclidean norm of the vector) as the square root of the inner-product of a vector with itself. The dot product of a vector gives the length of the vector squared.
#length of u
length_u <- sqrt(sum(u*u))
#length of v
length_v <- sqrt(sum(v*v))
linear_comb<-3*u−2*v
linear_comb
## [1] -4.5 9.5
Angle between two vectors: The dot-product between two vectors is proportional to their lengths and to the angle between them. In particular, the cosine of the angle between them. If you have two unit vectors, then the dot-product between them is simply the cosine of the angle between them.
angle <- acos(dot_prod/(length_u * length_v))
angle
## [,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. 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. Please note that you do have to worry about zero pivots, though. Please note 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]
solution <- function(a,b){
# add on the constraint vector as column to matrix
se <- cbind(a, b)
# gauss transform: for row 2
se[2,] <- -1 * se[2,1]/se[1,1] * se[1,] + se[2,]
# gauss transform: for row 3, col 1
se[3,] <- -1 * se[3,1]/se[1,1] * se[1,] + se[3,]
# gauss transform: for row 3, col 2
se[3,] <- -1 * se[3,2]/se[2,2] * se[2,] + se[3,]
# substitution
z <- se[3,4] / se[3,3]
y <- (se[2,4] - se[2,3] * z) / se[2,2]
x <- (se[1,4] - se[1,3] * z - se[1,2] * y) / se[1,1]
result <- as.vector(round(c(x,y,z), digits=2))
return(result)
}
# Running the function
A <- t(matrix(c(1,1,3,2,-1,5,-1,-2,4), ncol=3))
cons <- c(1,2,6)
solution(A,cons)
## [1] -1.55 -0.32 0.95