\[u = \left[ \begin{array}{cc}0.5 \\0.5 \end{array} \right]; \ v = \left[ \begin{array}{cc}3 \\-4 \end{array} \right]\]
Calculate the dot product \(u \cdot v\)
(dot_prod <- sum(u * v))
[1] -0.5
What are the lengths of \(u\) and \(v\)?
(len_u <- sqrt(sum(u * u)))
[1] 0.7071068
(len_v <- sqrt(sum(v * v)))
[1] 5
What is the linear combination \(3u - 2v\)?
3 * u - 2 * v
[1] -4.5 9.5
What is the angle between \(u\) and \(v\)?
\[\cos \theta = \frac{u \cdot v}{||u|| \ ||v||}\]
(theta <- acos(dot_prod / (len_u * len_v)))
[1] 1.712693
elim_solve <- function(A, b) {
#find pivot
pivot_1 <- A[1, 1]
#swap if pivot == 0
if (pivot_1 == 0) {
A <- A[c(2, 1, 3), ]
pivot_1 <- A[1, 1]
if (pivot_1 == 0) {
A <- A[c(3, 2, 1), ]
pivot_1 <- A[1, 1]
}
}
#multiplier & eliminate row 2
mult_2 <- A[2, 1] / pivot_1
A[2, ] <- A[2, ] - mult_2 * A[1, ]
b[2] <- b[2] - mult_2 * b[1]
#multiplier & eliminate row 3
mult_3 <- A[3, 1] / pivot_1
A[3, ] <- A[3, ] - mult_3 * A[1, ]
b[3] <- b[3] - mult_3 * b[1]
#find pivot
pivot_2 <- A[2, 2]
#swap if pivot == 0
if (pivot_2 == 0) {
A <- a[c(1, 3, 2), ]
pivot_2 <- A[2, 2]
}
#multiplier & eliminate row 3
mult_3 <- A[3, 2] / pivot_2
A[3, ] <- A[3, ] - mult_3 * A[2, ]
b[3] <- b[3] - mult_3 * b[2]
#solve for x3
x3 <- b[3] / A[3, 3]
#substitution
x2 <- (b[2] - A[2, 3] * x3) / A[2, 2]
x1 <- (b[1] - A[1, 3] * x3 - A[1, 2] * x2) / A[1, 1]
#results
x <- matrix(c(x1, x2, x3), nrow = 3)
x
}
Test the system \[\left[ \begin{array}{cc}1 & 1 & 3 \\2 & -1 & 5 \\-1 & -2 & 4 \end{array} \right] \left[ \begin{array}{cc}x_1 \\x_2 \\x_3 \end{array} \right] = \left[ \begin{array}{cc}1 \\2 \\6 \end{array} \right]\]
eq <- matrix(c(1, 1, 3, 2, -1, 5, -1, -2, 4), nrow = 3, ncol = 3, byrow = TRUE)
constr <- matrix(c(1, 2, 6), nrow = 3)
round(elim_solve(eq, constr), 2)
[,1]
[1,] -1.55
[2,] -0.32
[3,] 0.95