u = c(0.5, 0.5)
v = c(3, -4)
dot.prod = u %*% v
print(dot.prod)
## [,1]
## [1,] -0.5
length.u = sqrt(u %*% u)
length.v = sqrt(v %*% v)
print(length.u)
## [,1]
## [1,] 0.7071068
print(length.v)
## [,1]
## [1,] 5
lin.comb = (3 * u) - (2 * v)
print(lin.comb)
## [1] -4.5 9.5
angle <- function(x,y){
dot.prod <- x%*%y
norm.x <- norm(x,type="2")
norm.y <- norm(y,type="2")
theta <- acos(dot.prod / (norm.x * norm.y))
#convert from radians to degrees
theta = theta * (180/pi)
as.numeric(theta)
}
angle(u , v)
## [1] 98.1301
Source : https://stackoverflow.com/questions/16044377/how-to-do-gaussian-elimination-in-r-do-not-use-solve
substitute_solve <- function(A,B){
p <- nrow(A)
(U.pls <- cbind(A,b))
U.pls[1,] <- U.pls[1,]/U.pls[1,1]
for (i in 2:p){
for (j in i:p) {
U.pls[j, ] <- U.pls[j, ] - U.pls[i-1, ] * U.pls[j, i-1]
}
U.pls[i,] <- U.pls[i,]/U.pls[i,i]
}
for (i in p:2){
for (j in i:2-1) {
U.pls[j, ] <- U.pls[j, ] - U.pls[i, ] * U.pls[j, i]
}
}
U.pls
}
A <- matrix(c(1,1,3,2,-1,5,-1,-2,4), nrow = 3, ncol = 3, byrow = TRUE)
b <- matrix(c(1,2,6), nrow = 3, ncol = 1)
#Test our function
result = substitute_solve(A,b)
print(result[,4])
## [1] -1.5454545 -0.3181818 0.9545455
Verify Results
solve (A,b)
## [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,] 0.9545455