Question #1: Calculate the dot product \(u.v\) where \(u = [0.5, 0.5]\) and \(v = [3, -4]\).
To take the dot product we’ll use the formula: \(v.w = v_{1}w_{1} + v_{2}w_{2} + v_{3}w_{3} + ... + v_{n}w_{n}\)
Therefore, for our example we will do the following:
\(u.v = (0.5 * 3) + (0.5 * -4)\)
\(u.v = (1.5) + (-2)\)
\(u.v = -0.5\)
Answer: The dot product of \(u.v\) is -0.5.
Checking with R:
q1 <- (0.5 * 3) + (0.5 * -4)
q1
## [1] -0.5
Question #2: What are the lengths of \(u\) and \(v\)? Please note that the mathematical notion of length of a vector is not the same as a computer science definition.
In order to find the length of \(u\) and \(v\), we’ll have to use the following equation: \(||a|| = \sqrt{(\vec{a_{1}})^{2} + (\vec{a_{2}})^{2} }\)
Which for our question is:
\(||u|| = \sqrt{0.5^{2} + 0.5^{2}}\)
\(||u|| = \sqrt{0.25 + 0.25}\)
\(||u|| = \sqrt{0.5}\)
\(||u|| = 0.70710678\)
\(||v|| = \sqrt{3^{2} + -4^{2}}\)
\(||v|| = \sqrt{9 + 16}\)
\(||v|| = \sqrt{25}\)
\(||v|| = 5\)
Answer: The length of \(u\) is \(\sqrt{0.5}\) or approximately 0.707106. The length of \(v\) is 5.
Checking with R:
q2_u <- sqrt(0.5^2 + 0.5^2)
q2_v <- sqrt(3^2 + (-4*-4))
paste0('u is equal to ', q2_u)
## [1] "u is equal to 0.707106781186548"
paste0('v is equal to ', q2_v)
## [1] "v is equal to 5"
Question #3: What is the linear combination: \(3u - 2v\)?
To solve this, we substitute for \(u\) and \(v\):
\(=3[0.5, 0.5] - 2[3, -4]\)
\(=[1.5, 1.5] - [6, -8]\)
\(=[(1.5 - 6), (1.5 - -8)]\)
\(=[-4.5, 9.5]\)
Answer: The linear combination is [-4.5, 9.5].
Checking with R:
q3_1 <- c((3*0.5), (3*0.5))
q3_2 <- c((2*3), (2*-4))
q3_1 - q3_2
## [1] -4.5 9.5
Question #4: What is the angle between \(u\) and \(v\)?
To solve this problem, we can refer to Module 1, which mentions that the angle between two vectors can be found by determining the dot-product between two vectors and their lengths. Since they are proportional to one another, we also know that the cosine of the angle between them is also proportional to these two attributes. Therefore, we can use the equation:
\(cos \space \theta = \frac{u.v}{||\vec{u}|| \times ||\vec{v}||}\)
With this in mind, we’ve already found \(u.v = -0.5\), \(||\vec{u}|| = \sqrt{0.5}\), and \(||\vec{v}|| = 5\)
We can substitute these values into our equation:
\(cos \space \theta = \frac{-0.5}{\sqrt{0.5} \times 5}\)
\(cos \space \theta = \frac{-0.5}{3.5355}\)
\(cos \space \theta = -0.1414\)
To find the angle between u and v, we’ll then have to solve for theta:
\(\theta = cos^{-1}(-0.1414)\), which is also the arccosine of this number.
\(\theta = 1.712672 \space radians\)
Which can then be converted from radians to degrees by multiplying by \(\frac{180}{\pi}\):
\(\theta = 1.712672 \space radians \times \frac{180}{\pi}\)
\(\theta = 98.13 \space degrees\)
Answer: The angle between \(u\) and \(v\) is approximately 98.13 degrees.
Checking with R:
cos_theta <- (-0.5) / (sqrt(0.5) * 5)
theta <- acos(cos_theta)
theta_degrees <- theta * (180/pi)
paste0('The angle is about ', round(theta_degrees, digits = 2), ' degrees.')
## [1] "The angle is about 98.13 degrees."
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.
echelon <- function(a, b) {
#zero pivot check/switch
if(a[2,2] == 0) {
if(a[3,2] == 0){
print('There is either no solution or infinite solutions -- further calculations necessary')
} else {
# swap rows to get rid of zero pivot
temp_r2 <- a[2,]
a[2,] <- a[3,]
a[3,] <- temp_r2
temp_r3 <- b[2,]
b[2,] <- b[3,]
b[3,] <- temp_r3
}
}
# Check to see if there's a one in the first pivot
if(a[1, 1] != 1) {
mult_init <- a[1, 1]
a[1,] <- a[1,] / mult_init
b[1,] <- b[1,] / mult_init
}
# First zero (R2)
if(a[2, 1] != 0) {
mult_1 <- a[2, 1]
a[2,] <- a[2,] - (a[1,] * mult_1)
b[2,] <- b[2,] - (b[1,] * mult_1)
}
# Second zero (R3)
if(a[3, 1] != 0){
mult_2 <- a[3, 1]
a[3,] <- a[3,] - (a[1,] * mult_2)
b[3,] <- b[3,] - (b[1,] * mult_2)
}
# Third zero (R3)
if(a[3, 2] != 0){
mult_3 <- a[2, 2]
mult_4 <- a[3, 2]
a[3,] <- (a[3,] * mult_3) - (a[2,] * mult_4)
b[3,] <- (b[3,] * mult_3) - (b[2,] * mult_4)
}
# solve for z, then y, then x (back-substitution)
if(a[3, 3] == 1){
z <- b[3, 1]
y <- ((b[2, 1]) - (a[2, 3]*z))/ (a[2, 2])
x <- b[1, 1] - (a[1, 2]*y) - (a[1, 3]*z)
} else {
mult_5 <- a[3, 3]
a[3, 3] <- a[3, 3] / mult_5
b[3, 1] <- b[3, 1] / mult_5
z <- b[3, 1]
y <- ((b[2, 1]) - (a[2, 3]*z))/ (a[2, 2])
x <- b[1, 1] - (a[1, 2]*y) - (a[1, 3]*z)
}
final_matrix <- matrix(c(round(x, digits = 2), round(y, digits = 2), round(z, digits = 2)), nrow = 1, ncol = 3)
print(final_matrix)
}
Solution to the problem set using my “echelon()” function:
mat3 <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), ncol = 3, nrow = 3)
mat4 <- matrix(c(1, 2, 6), ncol = 1, nrow = 3)
echelon(mat3, mat4)
## [,1] [,2] [,3]
## [1,] -1.55 -0.32 0.95
Checking my answer based on the built-in R function:
solve(mat3, mat4)
## [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,] 0.9545455
Tests on zero pivots using my “echelon()” function:
mat7 <- matrix(c(1, 0, 0, -1, 0, 2, 2, -1, -1), ncol = 3, nrow = 3)
mat8 <- matrix(c(8, -11, -3), ncol = 1, nrow = 3)
echelon(mat7, mat8)
## [,1] [,2] [,3]
## [1,] -10 4 11
mat14 <- matrix(c(2, 0, 0, 5, 0, 1, 1, -1, -1), ncol = 3, nrow = 3)
mat15 <- matrix(c(0, 2, 3), ncol = 1, nrow = 3)
echelon(mat14, mat15)
## [,1] [,2] [,3]
## [1,] -1.5 1 -2
Checking these answers with the built-in R function:
solve(mat7, mat8)
## [,1]
## [1,] -10
## [2,] 4
## [3,] 11
solve(mat14, mat15)
## [,1]
## [1,] -1.5
## [2,] 1.0
## [3,] -2.0
Tests with one in the first pivot using my “echelon()” function:
mat1 <- matrix(c(1, 3, 0, 2, 8, 4, 1, 1, 1), ncol = 3, nrow = 3)
mat2 <- matrix(c(2, 12, 2), ncol = 1, nrow = 3)
echelon(mat1, mat2)
## [,1] [,2] [,3]
## [1,] 2 1 -2
mat11 <- matrix(c(1, 2, -3, -2, 1, 2, 3, 1, -2), ncol = 3, nrow = 3)
mat12 <- matrix(c(7, 4, -10), ncol = 1, nrow = 3)
echelon(mat11, mat12)
## [,1] [,2] [,3]
## [1,] 2 -1 1
Checking these answers with the built-in R function:
solve(mat1, mat2)
## [,1]
## [1,] 2
## [2,] 1
## [3,] -2
solve(mat11, mat12)
## [,1]
## [1,] 2
## [2,] -1
## [3,] 1
Tests without one in the first pivot using my “echelon()” function:
# not one in A1
mat5 <- matrix(c(2, 4, -2, 4, 9, -3, -2, -3, 7), ncol = 3, nrow = 3)
mat6 <- matrix(c(2, 8, 10), ncol = 1, nrow = 3)
echelon(mat5, mat6)
## [,1] [,2] [,3]
## [1,] -1 2 2
mat9 <- matrix(c(2, 4, -2, -4, -1, 2, 5, 0, -3), ncol = 3, nrow = 3)
mat10 <- matrix(c(-33, -5, 19), ncol = 1, nrow = 3)
echelon(mat9, mat10)
## [,1] [,2] [,3]
## [1,] -0.5 3 -4
Checking these answers with the built-in R function:
solve(mat5, mat6)
## [,1]
## [1,] -1
## [2,] 2
## [3,] 2
solve(mat9, mat10)
## [,1]
## [1,] -0.5
## [2,] 3.0
## [3,] -4.0
After doing a fair amount of tests on my “echelon()” function, it appears to satisfy all the requirements for Problem Set #2.