Problem 1

Question 1

Calculate the dot productu:v where u= [0.5; 0.5] and v= [3;-4]

u <- c(0.5,0.5)

v <- c(3,-4)

dot_product <- u %*% v

Question 2

What are the lengths of uand v? Please note that the mathematical notion of the length of a vector is not the same as a computer science de notion.

len(u) # length of u
## [1] 0.7071068
len(v) # length of v
## [1] 5

Question 3

What is the linear combination: 3u - 2v?

sum <- 3*u-2*v
sum # For the scaled vector
## [1] -4.5  9.5
xlim <- c(-5,5)
ylim <- c(0,10)
# proper geometry requires asp=1
plot( xlim, ylim, type="n", xlab="X", ylab="Y", asp=1)
abline(v=0, h=0, col="gray")

vectors(3*u, origin=c(0,0), col="red", lty=2)
vectors(-2*v, origin=c(0,0), col="blue", lty=2)
vectors(sum, origin=c(0,0), col="grey", lty=2)
vectors(sum, origin=3*u, col="grey", lty=2)
vectors(sum, origin=-2*v, col="grey", lty=2)

Question 4

What is the angle between u and v?

angle_radians <- acos((u %*% v)/(len(u)*len(v)))

angle_degree <- (angle_radians * 180) / (pi)

angle_degree # in Degree
##         [,1]
## [1,] 98.1301

2.Problem set 2

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 functionsolveto 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 theGauss Jordan elimination procedure.

#Option 1:

u1 <- c(1,2,-1) #red
v1 <- c(1,-1,-2) #Green
w1 <- c(3,5,4) #blue
b  <- c(1,2,6)
x = numeric(3)

uvz1_aug <- matrix(c(u1,v1,w1),ncol=3)


  
solve_elimination <- function(A,b) {
  E1 <- matrix(c(1,0,0,0,1,0,0,0,1),ncol=3)
  E2<-  matrix(c(1,0,0,0,1,0,0,0,1),ncol=3)
  E3 <- matrix(c(1,0,0,0,1,0,0,0,1),ncol=3)

  uvz1_aug <- matrix(c(A,b),ncol=4)

  
  if(uvz1_aug[1,1] !=0) {
  if((uvz1_aug[2,1]-uvz1_aug[2,1]*uvz1_aug[1,1]) ==0) {
    E1[2,1] <- -1*uvz1_aug[2,1]
  }
  } else {
    uvz1_aug <- uvz1_aug[c(2,1,3)]
    
  }
  
   if(uvz1_aug[2,1] !=0) {
   if((uvz1_aug[3,1] - uvz1_aug[3,1]*uvz1_aug[1,1]) ==0) {
  E2[3,1] <- -1*uvz1_aug[3,1]
}
  } else {
    uvz1_aug <- uvz1_aug[c(2,1,3)]
    
  }
    
  if((uvz1_aug[3,2]-uvz1_aug[3,2]*uvz1_aug[1,2]) ==0) {
    E3[3,2] <- uvz1_aug[3,2]
  }
   
    output <- E3%*%  E2 %*% E1 %*%uvz1_aug
   
    output[2,] <- output[2,]/output[2,2]
    output[3,] <- output[3,]/output[3,2]
    output[3,] <- output[3,]-output[2,]
    
  x[3] <- output[3,4] / output[3,3]
  x[2] <- (output[2,4] - output[2,3]*x[3]) / output[2,2]
  x[1] <- (output[1,4] - output[1,2]*x[2] - output[1,3]*x[3]) / output[1,1]

  return(x)
  }

solve_elimination(uvz1_aug,b)
## [1] -1.5454545 -0.3181818  0.9545455

Option 2:

#Option2
uvz1 <- matrix(c(u1,v1,w1),ncol=3)

rownames(uvz1) <- c("U", "V", "W")

vec <- rbind(matrix(c(u1,v1,w1),ncol=3), c(b))
rownames(vec) <- c("X", "Y", "Z", "J")
solve(uvz1,b)
## [1] -1.5454545 -0.3181818  0.9545455
open3d()
## wgl 
##   1
plotEqn3d(uvz1,b)
vectors3d(vec, color=c(rep("black",3), "red"), lwd=2)