A: (1) Calculate the dot product u.v where u = [0.5; 0.5] and v = [3; -4]
#Initiate the vector
u = c(0.5,0.5)
v = c(3,-4)
#dot product
result = u%*%v
Dot product of vectors: -0.5
(2) What are the lengths of u and v? Please note that the mathematical notion of the length of a vector is not the same as a computer science definition.
#Initiate the vector
u = c(0.5,0.5)
#length of u
result = sqrt(u%*%u)
Length of u: 0.7071068
v = c(3,-4)
#length of v
result = sqrt(v%*%v)
Length of v: 5
(3) What is the linear combination: 3u - 2v?
\(3\left[\begin{array}{cc}0.5 & 0.5\end{array}\right] - 2\left[\begin{array}{cc}3 & -4\end{array}\right]\)
u = c(0.5,0.5)
v = c(3,-4)
result = (3*u) - (2*v)
Linear combination: [-4.5, 9.5]
(4) What is the angle between u and v?
\(cos\theta = \frac{dot\ product\ of\ u.v}{(magnitude\ of\ u) * (magnitude\ of\ v)}\)
\(cos\theta = \frac{u.v}{||u|| * ||v||}\)
u = c(0.5,0.5)
v = c(3,-4)
dotproduct = u%*%v
magnitudeu = sqrt(u%*%u)
magnitudev = sqrt(v%*%v)
costheta = dotproduct / (magnitudeu * magnitudev)
theta = acos(costheta)
\(cos\theta =\) -0.1414214
\(\theta = cos^-1(\frac{u.v}{||u|| * ||v||})^0\)
Angle between vectors (\(\theta\)): 1.7126934 \(^0\)
A:
GJ_linear_eq <- function(A, b){
#initiate result matrix
x = matrix(c(0,0,0), nrow = 3)
#bind both matrices
res = cbind(A, b)
#Check if first row first value is 0 if so swap the rows
if (res[1,1] == 0){
res = res[c(2,1,3),]
}
#Check if second row second value is 0 if so swap the rows
if (res[2,2] == 0){
res = res[c(3,2,1),]
}
#Check if second row first value is 0 if not make it zero by
#multiplying first row first value with second row and second row first value with first row
#then substract resultant first row from resultant second row
if (res[2,1] != 0){
res1 = (res[2,1] * res[1,])
res2 = (res[1,1] * res[2,])
res[2,] = res1 - res2
}
#repeat above process for all the values
if (res[3,1] != 0){
res1 = (res[3,1] * res[1,])
res2 = (res[1,1] * res[3,])
res[3,] = res1 - res2
}
if (res[3,2] != 0){
res1 = (res[3,2] * res[2,])
res2 = (res[2,2] * res[3,])
res[3,] = res1 - res2
}
if (res[1,2] != 0){
res1 = (res[2,2] * res[1,])
res2 = (res[1,2] * res[2,])
res[1,] = res1 - res2
}
if (res[1,3] != 0){
res1 = (res[3,3] * res[1,])
res2 = (res[1,3] * res[3,])
res[1,] = res1 - res2
}
if (res[2,3] != 0){
res1 = (res[3,3] * res[2,])
res2 = (res[2,3] * res[3,])
res[2,] = res1 - res2
}
#Once matrix is result with diagonal values, and above and below values are zero, divide each row by its non zero value
res[1,] = res[1,]/res[1,1]
res[2,] = res[2,]/res[2,2]
res[3,] = res[3,]/res[3,3]
#Values left in 4 column are results
x[1] = res[1,4]
x[2] = res[2,4]
x[3] = res[3,4]
return(x)
}
A = matrix(c(1,2,-1,1,-1,-2,3,5,4), nrow=3, ncol=3)
b = matrix(c(1, 2, 6), nrow = 3)
result = GJ_linear_eq(A, b)
Value of \({x}_1\) = -1.5454545, \({x}_2\) = -0.3181818, \({x}_3\) = 0.9545455