Solution: Solution-1 - using raw calculation: u.v = u1.v1 + u2.v2 = (0.5 x 3) + (0.5 x -4) =1.5 - 2 = -0.5
Solution-2 - using R:
u <- c(0.5,0.5)
v <- c(3,-4)
dot.prod.assign.1 <- sum(u*v)
sprintf("dot product of u and v is : %s", dot.prod.assign.1)
## [1] "dot product of u and v is : -0.5"
Using R:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
u.length <- sum(u*u) %>% sqrt()
v.length <- sum(v*v) %>% sqrt()
sprintf("Length of u is : %s", u.length)
## [1] "Length of u is : 0.707106781186548"
sprintf("Length of v is : %s", v.length)
## [1] "Length of v is : 5"
Solution-2 - Using R
linear.combination.u.v <- 3*u - 2*v
sprintf("Linear combination 3u - 2v is : %s", linear.combination.u.v)
## [1] "Linear combination 3u - 2v is : -4.5"
## [2] "Linear combination 3u - 2v is : 9.5"
theta_rad <- acos(dot.prod.assign.1 / (u.length * v.length))
theta_deg <- (theta_rad * 180) / pi
sprintf("Angle between 2 vectors in degrees: %s", theta_deg)
## [1] "Angle between 2 vectors in degrees: 98.130102354156"
### 3X3 matrix
threeby3.matrix.function <- function(a,b)
{
### To make a[2,1] = 0, Do this: a[2,1] = 0 = a[2,1] - (a[2,1]/a[1,1]) * a[1,1]
### Hence multiplier in this case = a[2,1]/a[1,1], and this multiplier will be done on whole R2
### R2 = R2 - multiplier1 * R1
multiplier1 <- (a[2,1]/a[1,1])
a[2,1] <- 0
a[2,2] <- a[2,2] - (multiplier1 * a[1,2])
a[2,3] <- a[2,3] - (multiplier1 * a[1,3])
b[2] <- b[2] - multiplier1 * b[1]
### To make a[3,1] = 0, Do this: a[3,1] = 0 = a[3,1] - (a[3,1]/a[1,1]) * a[1,1]
### Hence multiplier in this case = a[3,1]/a[1,1], and this multiplier will be done on whole R3
### R3 = R3 - multiplier2 * R1
multiplier2 <- (a[3,1]/a[1,1])
a[3,1] <- 0
a[3,2] <- a[3,2] - (multiplier2 * a[1,2])
a[3,3] <- a[3,3] - (multiplier2 * a[1,3])
b[3] <- b[3] - (multiplier2 * b[1])
### Now we will make a[3,2] = 0, so that upper triangular form is created
### To make a[3,1] = 0, Do this: a[3,2] = 0 = a[3,2] - (a[3,2]/a[2,2]) * a[2,2]
### This ensures that a[3,1] also remains 0, as a[3,1] and a[2,1] are both zero from previous steps
### Hence multiplier in this case = a[3,2]/a[2,2], and this multiplier will be done on whole R3
### R3 = R3 - multiplier3 * R2
multiplier3 <- (a[3,2]/a[2,2])
a[3,2] <- 0
a[3,3] <- a[3,3] - (multiplier3 * a[2,3])
b[3] <- b[3] - (multiplier3 * b[2])
### x[3] can be calculated easily as below now as equation 3 has now only 1 variable x[3]
x <- vector(mode = "numeric", length = 3)
x[3] <- b[3] / a[3,3]
### Calculating x[1] and x[2] by back substitution now
x[2] <- (b[2] - a[2,3]*x[3])/a[2,2]
x[1] <- (b[1] - a[1,3]*x[3] - a[1,2]*x[2])/a[1,1]
return(x)
}
### Finding / testing the solution function
a3 <- matrix(c(1,2,-1,1,-1,-2,3,5,4),3,3)
b3 <- c(1,2,6)
a3
## [,1] [,2] [,3]
## [1,] 1 1 3
## [2,] 2 -1 5
## [3,] -1 -2 4
b3
## [1] 1 2 6
threeby3.matrix.function(a3,b3)
## [1] -1.5454545 -0.3181818 0.9545455