#define the vectors
v = c(1, 4, -3)
w = c(-2, 5, 7)
#add the vectors
v + w
## [1] -1 9 4
#compute dot product of the vectors
v%*%w
## [,1]
## [1,] -3
#Part 1
#Calculate the dot product of u and v
u <- c(0.5,0.5)
v <- c(3, -4)
u.v <- as.numeric(u%*%v)
u.v
## [1] -0.5
#Part 2
#Calculate the mathematical lengths of u and v
u.length <- sqrt(0.5^2 + 0.5^2)
v.length <- sqrt(3^2 + (-4)^2)
#Part 3
#Calculate 3u- 2v
(3*u - 2*v)
## [1] -4.5 9.5
#Part 4
#Calculate the angle between u and v
u.v.cos = u.v / (u.length*v.length)
u.v.cos
## [1] -0.1414214
u.v.acos <- acos(u.v.cos)
u.v.acos
## [1] 1.712693
library(NISTunits)
NISTradianTOdeg(u.v.acos)
## [1] 98.13012
\[ x + y + 3z = 1 \] \[ 2x -y + 5z = 2 \] \[ -x -2y + 4z = 6 \] Choosing two pairs of equations, we get: Pair 1: \[ x + y + 3z = 1 \] \[ 2x -y + 5z = 2 \] Pair 2: \[ x + y + 3z = 1 \] \[ -x -2y + 4z = 6 \] To solve for x, the first goal is to eliminate one of the variables from both of these pairs. We’ll choose to eliminate y first. In Pair 1, this is all set up for us. They cancel out, leaving us with: Pair 1 v2: \[ x + 3z = 1 \] \[ 2x + 5z = 2 \] Combining these, we get Pair 1 v3: \[3x + 8z = 3\] Now back to Pair 2. In order to eliminate the y variable, we need to multiply the first equation by 2. This will give us: \[2x + 2y + 6z = 2\] Adding that to our other Pair 2 equation of: \[-x - 2y + 4z = 6 \] We get Pair 2 v2: \[2x + 6z = 2 \] \[-x + 4z = 6\] Combining these, we get Pair 2 v3: \[x + 10z = 8\]
Next, we need to combine Pair 1 v3 and Pair 2 v3. Before this, we’re going to make our arrangements to eliminate the z variable. By multiplying Pair 1 v3 by 10, and Pair 2 v3 by -8, we get the following: \[-8x - 80z = -64\] \[30x + 80z = 30\] Adding these together, we get: \[22x = -34\] Which brings us to our final answer of: \[x = -1.55\]
a <- matrix(c(1,1,3,2,-1,5,-1,-2,4), byrow=T, nrow=3, ncol=3)
b <- matrix(c(1,2,6), nrow=3, ncol=1)
p <- nrow(a)
(A.b <- cbind(a,b))
## [,1] [,2] [,3] [,4]
## [1,] 1 1 3 1
## [2,] 2 -1 5 2
## [3,] -1 -2 4 6
A.b[1,] <- A.b[1,] / A.b[1,1]
i <- 2
while (i < p + 1) {
j <- i
while (j < p + 1) {
A.b[j, ] <- A.b[j, ] - A.b[i - 1, ] * A.b[j, i - 1]
j <- j + 1
}
while (A.b[i,i] == 0) {
A.b <- rbind(A.b[-i,],A.b[i,])
}
A.b[i,] <- A.b[i,] / A.b[i,i]
i <- i + 1
}
for (i in p:2){
for (j in i:2-1) {
A.b[j, ] <- A.b[j, ] - A.b[i, ] * A.b[j, i]
}
}
round(A.b, 2)
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 -1.55
## [2,] 0 1 0 -0.32
## [3,] 0 0 1 0.95
#validate that the results are correct
solve(a, b)
## [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,] 0.9545455