u = 0.5i + 0.5j
v = 3i -4j
u.v = 0.5*3 + 0.5*(-4)
u.v
## [1] -0.5
lengths_of_v = sqrt(0.5^2 + 0.5^2)
lengths_of_u = sqrt(3^2 + (-4)^2)
lengths_of_v
## [1] 0.7071068
lengths_of_u
## [1] 5
u <- matrix(c(0.5,0.5))
v <- matrix(c(3, -4))
(3*u) - (2*v)
## [,1]
## [1,] -4.5
## [2,] 9.5
u.v = lengths_of_v * lengths_of_u * cos(theta)
cos_theta = u.v/(lengths_of_v * lengths_of_u)
theta = acos(cos_theta) * 180/pi
theta
## [1] 98.1301
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.
#create testing matrix 3x3
A <- array(c(1, 12, -44, 1, -1, -2, 3, 5, 4),dim=c(3,3))
#create a testing vector
b <- array(c(1,5,6),dim=c(3,1))
elimination_method <- function(A, b) {
#bind A and b to create an augmented matrix
aug_matrix <- cbind(A,b)
print(aug_matrix)
#devide the first row by the cooficient that is located in the first row and the first column
aug_matrix[1,] <- aug_matrix[1,]/aug_matrix[1,1]
#the cooficient [1,1] should be equal 1
print(aug_matrix)
#start with the second row
row <- 2
#iterate through all rows in augmented matrix
while (row < nrow(A)+1) {
#assign row number to column
col <- row
#iterate through all columns in augmented matrix
while (col < nrow(A) + 1) {
# multiply the previous row by coefficient located in the current row and the column that equals the number of previous row and subtract it from the current row
aug_matrix[col, ] <- aug_matrix[col, ] - aug_matrix[row-1, ] * aug_matrix[col, row-1]
#go to the next column
col <- col+1
}
#bind augmented matrix that contain current row only with augmented matrix that doesn't contain current row while the coefficient that is located in [current row,current row] equals 0
while (aug_matrix[row,row] == 0) {
aug_matrix <- rbind(aug_matrix[-row,],aug_matrix[row,])
}
aug_matrix[row,] <- aug_matrix[row,]/aug_matrix[row,row]
#go to the next row
row <- row + 1
}
#the coefficients located in [1,1],[2,2],[3,3] etc. (where row=column) should equal to 1
print(aug_matrix)
#iterate through rows from last row to the second row
for (row in nrow(A):2){
#iterate through columns (from the first)
for (col in row:2 -1) {
# multiply the current row by coefficient located in the same row and current column that and subtract it from the row that equals to the current column
aug_matrix[col, ] <- aug_matrix[col, ] - aug_matrix[row, ] * aug_matrix[col, row]
}
}
#the coefficients located in [1,1],[2,2],[3,3] etc. (where row=column) should equal to 1 while all other coefficients shoud equal to 0
print(aug_matrix)
}
Now, let’s test the function elimination_method with the data provided in the problem.
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))
elimination_method(A, b)
## [,1] [,2] [,3] [,4]
## [1,] 1 1 3 1
## [2,] 2 -1 5 2
## [3,] -1 -2 4 6
## [,1] [,2] [,3] [,4]
## [1,] 1 1 3 1
## [2,] 2 -1 5 2
## [3,] -1 -2 4 6
## [,1] [,2] [,3] [,4]
## [1,] 1 1 3.0000000 1.0000000
## [2,] 0 1 0.3333333 0.0000000
## [3,] 0 0 1.0000000 0.9545455
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 -1.5454545
## [2,] 0 1 0 -0.3181818
## [3,] 0 0 1 0.9545455
Let’s compare results generated by the function ‘elimination_method’ with built-in R function ‘solve’.
solve(A,b)
## [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,] 0.9545455
The results are equal.