PROBLEM SET 1
u <- c(.5,.5)
v <- c(3,-4)
# 1
dotproduct <- u%*%v
dotproduct
## [,1]
## [1,] -0.5
#-0.5
# 2
length_u <- sqrt((.5)^2+(.5)^2)
length_u
## [1] 0.7071068
#0.707107
#
length_v <- sqrt((3)^2+(-4)^2)
length_v
## [1] 5
#5
# 3
comb <- 3*u + 2*v
comb
## [1] 7.5 -6.5
#(7.5, -6,5)
# 4
theta <- (dotproduct)/(sqrt(.5^2+.5^2)+sqrt(3^2+(-4)^2))
degrees <- acos(theta) * (180/pi)
degrees
## [,1]
## [1,] 95.02613
#95.02613 degrees
PROBLEM SET 2
#Matrix and vector that will be entered as variables into the function
matrixA <- matrix(c(1,2,-1,1,-1,-2,3,5,4), ncol = 3)
vectorB <- c(1,2,6)
upperTriangleSolution <- function(matrixA, vectorB)
{
#Check for Zero Pivot
#If a row has a leading zero, it will swap with a non-leading zero row
#If the first column is all rows, the function will end
if(matrixA[1,1] != 0){
result <- "TRUE"
}else
{
if(matrixA[1,1] == 0 & matrixA[2,1] != 0)
{
tempRow <- matrixA[2,]
matrixA[2,] <- matrixA[1,]
matrixA[1,] <- tempRow
result <- "TRUE"
}else if(matrixA[1,1] == 0 & matrixA[3,1] != 0)
{
tempRow <- matrixA[3,]
matrixA[3,] <- matrixA[1,]
matrixA[1,] <- tempRow
result <- "TRUE"
}else{result <- "FALSE"}
}
#If there is a non-zero in the first column, the function
#will construct an upper triangle matrix
if(result == "TRUE")
{
#Construct Upper Triangle Matrix by finding the coefficient
#to multiply by the first row and adding to the second row
#The first two numbers in column one are compared
a <- matrixA[1,1]
b <- matrixA[2,1]
getZero <- -(b/a)
matrixA[2,] <- (matrixA[1,] * getZero) + matrixA[2,]
#Comparing first and third numbers in the first column
b <- matrixA[3,1]
getZero <- -(b/a)
matrixA[3,] <- (matrixA[1,] * getZero) + matrixA[3,]
#At this point only the first number in the first column is a non-zero
#The second and third numbers of the second column are compared, multiplied, and added
a <- matrixA[2,2]
b <- matrixA[3,2]
getZero <- -(b/a)
matrixA[3,] <- (matrixA[2,] * getZero) + matrixA[3,]
#Backwards Substitution to solve for X variables
getX3 <- vectorB[3]/matrixA[3,3]
getX2 <- (vectorB[2]-(matrixA[2,3]*getX3))/matrixA[2,2]
getX1 <- (vectorB[1]-(matrixA[1,2]*getX2)-(matrixA[1,3]*getX3))/matrixA[1,1]
print("Upper Triangle Matrix is: ")
print(matrixA)
message("X1 = ", getX1)
message("X2 = ", getX2)
message("X3 = ", getX3)
}else
{
print("Can't be computed. 1st column is all zeros")
print(matrixA)
}
}