library(matlib)
## Warning: package 'matlib' was built under R version 3.4.4
#create and display the u vector
cat("Vector U","\n")
## Vector U
(u <- c(0.5,0.5))
## [1] 0.5 0.5
#create and display the v vector
cat("\n","Vector V","\n")
##
## Vector V
(v <- c(3,-4))
## [1] 3 -4
#concatenate and print
cat("\n","Dot Product Result","\n")
##
## Dot Product Result
#run the dot product
u %*% v
## [,1]
## [1,] -0.5
#length of vector u
cat("Length of Vector u","\n")
## Length of Vector u
sqrt(sum(u * u))
## [1] 0.7071068
#length of vector v
cat("\n","Length of Vector v","\n")
##
## Length of Vector v
sqrt(sum(v * v))
## [1] 5
#mutiplying a vector by a number
cat("Result of vector u multiplied by a scalar of 3","\n")
## Result of vector u multiplied by a scalar of 3
3 * u
## [1] 1.5 1.5
cat("\n","Result of vector v multiplied by a scalar of 2","\n")
##
## Result of vector v multiplied by a scalar of 2
2 * v
## [1] 6 -8
cat("\n","Linear combination of 3u - 2v","\n")
##
## Linear combination of 3u - 2v
(3*u) - (2*v)
## [1] -4.5 9.5
Using the matlib package the answer could be quickly found using the angle function.
#angle calculates the angle between two vectors.
#angle(x, y, degree = TRUE)
angle(u,v)
## [,1]
## [1,] 98.1301
The answer can also be calculated by using the acos function and applying it to the dot product of vectors u and v which are divided by the length of each respective vector.
#acos fucntion returns the radian arccosine of number data
cat("Radian calculation","\n")
## Radian calculation
acos(sum(u %*% v)/(sqrt(sum(u * u))*sqrt(sum(v * v))))
## [1] 1.712693
Next, we multiply the calculation to 180 and then divide by Pi. According to www.rapidtables.com Piradians are equal to 180 degrees.
1.712693*180/pi
## [1] 98.13008
Create the matrix & vector
cat("Matrix A","\n")
## Matrix A
rownames = c("R1","R2","R3")
colnames = c("C1","C2","C3")
(A <- matrix(c(1,1,3,
2,-1,5,
-1,-2,4),ncol=3,byrow=TRUE, dimnames=list
(rownames,colnames)))
## C1 C2 C3
## R1 1 1 3
## R2 2 -1 5
## R3 -1 -2 4
cat("\n","Vector B","\n")
##
## Vector B
(b <- c(1,2,6))
## [1] 1 2 6
cat("\n","Problem Set 2 - Matrix","\n")
##
## Problem Set 2 - Matrix
(ps2 <- cbind(A,b))
## C1 C2 C3 b
## R1 1 1 3 1
## R2 2 -1 5 2
## R3 -1 -2 4 6
First, we multiply -2 to Row 1 and then add the results to Row 2
cat("Row 2 of Problem Set 2","\n")
## Row 2 of Problem Set 2
(ps2[2,] <- -2*ps2[1,] + ps2[2,])
## C1 C2 C3 b
## 0 -3 -1 0
cat("\n","Problem Set 2","\n")
##
## Problem Set 2
ps2
## C1 C2 C3 b
## R1 1 1 3 1
## R2 0 -3 -1 0
## R3 -1 -2 4 6
Next, we multiply 1 to Row 1 and then add the results to Row 3
cat("Row 3 of Problem Set 2","\n")
## Row 3 of Problem Set 2
(ps2[3,] <- 1*ps2[1,] + ps2[3,])
## C1 C2 C3 b
## 0 -1 7 7
cat("\n","Problem Set 2","\n")
##
## Problem Set 2
ps2
## C1 C2 C3 b
## R1 1 1 3 1
## R2 0 -3 -1 0
## R3 0 -1 7 7
Using the rowswap from the matlib library, Row2 is switched with Row3
(ps2 <- rowswap(ps2,2,3))
## C1 C2 C3 b
## R1 1 1 3 1
## R2 0 -1 7 7
## R3 0 -3 -1 0
Next, we multiply Rows 2 and Rows 3 by 1
cat("Row 2 of Problem Set 2","\n")
## Row 2 of Problem Set 2
(ps2[2,] <- -1*ps2[2,])
## C1 C2 C3 b
## 0 1 -7 -7
cat("\n","Problem Set 2","\n")
##
## Problem Set 2
ps2
## C1 C2 C3 b
## R1 1 1 3 1
## R2 0 1 -7 -7
## R3 0 -3 -1 0
cat("\n","Row 3 of Problem Set 2","\n")
##
## Row 3 of Problem Set 2
(ps2[3,] <- -1*ps2[3,])
## C1 C2 C3 b
## 0 3 1 0
cat("\n","Problem Set 2","\n")
##
## Problem Set 2
ps2
## C1 C2 C3 b
## R1 1 1 3 1
## R2 0 1 -7 -7
## R3 0 3 1 0
Portion of the problem where I need some assistance
Using the solve method
outcome <- (solve(A) %*% b)
round(outcome,digits=2)
## [,1]
## C1 -1.55
## C2 -0.32
## C3 0.95