You can think of vectors representing many dimensions of related information. For
instance, Netflix might store all the ratings a user gives to movies in a vector. This is
clearly a vector of very large dimensions (in the millions) and very sparse as the user might
have rated only a few movies. Similarly, Amazon might store the items purchased by a user
in a vector, with each slot or dimension representing a unique product and the value of the
slot, the number of such items the user bought. One task that is frequently done in these
settings is to find similarities between users. And, we can use dot-product between vectors
to do just that. As you know, the dot-product is proportional to the length of two vectors
and to the angle between them. In fact, the dot-product between two vectors, normalized
by their lengths is called as the cosine distance and is frequently used in recommendation
engines.
\(u \cdot v= u_1 \times v_1 + u_2 \times v_2 = 0.5 \times 3 + 0.5 \times -4 = -0.5\)
## [,1]
## [1,] -0.5
\(||u|| = \sqrt{(u_1)^2 + (u_2)^2} = \sqrt{0.5^2 + 0.5^2} = \sqrt{0.25 + 0.25} = \sqrt{0.5} = 0.7071068\)
## [1] 0.7071068
\(||v|| = \sqrt{(v_1)^2 + (v_2)^2} = \sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5\)
## [1] 5
\(3u - 2v = [1.5, 1.5] - [6, 8] = [-4.5, 9.5]\)
## [1] -4.5 9.5
## [,1]
## [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. 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.
A = matrix(c(1,2,-1,1,-1,-2,3,5,4),nrow=3,ncol = 3)
b = c(1,2,6)
mySolver = function(A){
# Use the cbind function to combine the two matricies
A = cbind(A,b)
#Get the pivot, equal to the first element in the row greater than zero. This is accomplished by applying the pivot multiplier to the rows to eliminate variables
pm = (A[2,1]/A[1,1]*(A[1,]))
A[2,] = A[2,]-pm
pm = (A[3,1]/A[1,1]*(A[1,]))
A[3,] = A[3,]-pm
pm = (A[3,2]/A[2,2]*(A[2,]))
A[3,] = A[3,]-pm
# Do substitutions
l = (A[3,4]/A[3,3])
h = (A[2,4]-A[2,3]*l)/A[2,2]
m = (A[1,4]-A[1,3]*l - A[1,2]*h)/A[1,1]
answer <- c(m,h,l)
return(answer)
}
mySolver(A)## b b b
## -1.5454545 -0.3181818 0.9545455
Reference: MIT 18.06 Linear Algebra, Spring 2005 Instructor: Gilbert Strang Elimination Matrix: https://www.youtube.com/watch?v=QVKj3LADCnA&t=2258s View the complete course: http://ocw.mit.edu/18-06S05 YouTube Playlist: https://www.youtube.com/playlist?list…