U = c(.5,.5)
V = c(3,-4)
crossprod(V,U)
## [,1]
## [1,] -0.5
2.What are the lengths of u and v?
vectorlength = function(x){
return(sqrt((x[1]**2)+x[2]**2))
}
vectorlength(U)
## [1] 0.7071068
vectorlength(V)
## [1] 5
3.What is the linear combination of 3u - 2v?
(3*U) - (2*V)
## [1] -4.5 9.5
4.What is the angle between u and v?
vectorangle = function(U, V){
return( acos( (crossprod(U,V) / ((vectorlength(U)) * (vectorlength(V) ))))[1] * (180/3.1416))
}
vectorangle(U,V)
## [1] 98.12987
Problem set two; disregarding degenerative equations and zero pivots, create a function to solve a 3 variable, 3 constraint system.
A = matrix(c(1,2,-1,1,-1,-2,3,5,4),nrow=3,ncol = 3)
b = c(1,2,6)
solve = function(A){
# Combine into one matrix
A = cbind(A,b)
#Perform Gaussian Elimination
multiplier = (A[2,1]/A[1,1]*(A[1,]))
A[2,] = A[2,]-multiplier
multiplier = (A[3,1]/A[1,1]*(A[1,]))
A[3,] = A[3,]-multiplier
multiplier = (A[3,2]/A[2,2]*(A[2,]))
A[3,] = A[3,]-multiplier
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]
return(c(m,h,l))
}
solve(A)
## b b b
## -1.5454545 -0.3181818 0.9545455