Sol:
u <- c(0.5, 0.5)
v <- c(3, -4)
The dot product is -0.5.
Sol:
The length of u is 0.7071068 and length of v is 5.
Sol:
The linear combination is -4.5, 9.5.
Sol: The angle between u and v is 1.7126934
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]
\[ \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} \]
Create Function
sol <- function(A, b){
#Pivot
pivot <- A[1,1]
#exchange rows where pivot = 0
if (pivot == 0){
A <- A[c(2,1,3),]
pivot <- A[1,1]
if (pivot == 0){
A <- A[c(3,2,1),]
pivot <- A[1,1]
}
}
#Multiplier
mplyr_1 <- A[2,1]/pivot
mplyr_2 <- A[3,1]/pivot
#Elimination: row2
A[2, ] <- A[2, ] - mplyr_1 * A[1, ]
b[2,] <- b[2,] - mplyr_1 * b[1,]
#Elimination: row3
A[3, ] <- A[3, ] - mplyr_2 * A[1, ]
b[3,] <- b[3,] - mplyr_2 * b[1,]
#Repeat
pivot_1 <- A[2,2]
#exchange rows where pivot = 0
if (pivot_1 == 0){
A <- A[c(1,3,2),]
pivot_1 <- A[2,2]
}
#Multiplier
mplyr_3 <- A[3,2]/pivot_1
#Elimination: row3
A[3, ] <- A[3, ] - mplyr_3 * A[2, ]
b[3,] <- b[3,] - mplyr_3 * b[2,]
#x3 solution
x3 <- b[3] / A[3, 3]
#substitution1
x2 <- (b[2] - A[2, 3] * x3) / A[2, 2]
#substitution2
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 Function
A <- array(c(1, 2, -1, 1, -1, -2, 3, 5, 4), dim = c(3,3))
b <- array(c(1, 2, 6), dim = c(3,1))
round(sol(A, b), 2)
## [,1]
## [1,] -1.55
## [2,] -0.32
## [3,] 0.95