IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - FALL 2015

Assignment 01

Problem set 1
(1) Calculate the dot product u.v where u = [0.5; 0.5] and v = [3; -4]
(2) What are the lengths of u and v?
(3) What is the linear combination: 3u ??? 2v?
(4) What is the angle between u and v
u = c(0.5, 0.5)
v = c(3, -4)

# 1- dot product
u%*%v
##      [,1]
## [1,] -0.5
# 2- What are the lengths of u and v?

sqrt(u%*%u)
##           [,1]
## [1,] 0.7071068
sqrt(v%*%v)
##      [,1]
## [1,]    5
# 3- What is the linear combination: 3u ??? 2v?

lc<- 3*u - 2*v
lc
## [1] -4.5  9.5
# 4- What is the angle between u and v
# let ?? the angel between u and v.

# then cos(??) =  dot product of u and v / lentgh(u) times length(v)

Cosine_Angle <- (u%*%v) / ((sqrt(u%*%u)) * (sqrt(v%*%v)))
Angle<-  acos((u%*%v) / ((sqrt(u%*%u)) * (sqrt(v%*%v))))
Angle
##          [,1]
## [1,] 1.712693
# or we can write function 

angle <- function(x,y){
  dot.prod <- x%*%y 
  norm.x <- norm(x,type="2")
  norm.y <- norm(y,type="2")
  theta <- acos(dot.prod / (norm.x * norm.y))
  as.numeric(theta)
}

angle(u,v)
## [1] 1.712693

Problem set 2

Set up a system of equations with 3 variables and 3 constraints and solve for x. Please
write a function in Octave 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 test it with the system below and it should produce a solution x = [???1.55, ???0.33, 0.95]
solve_matrix <- function (A,b){

  # Checkin if the input values are matrices
  if (!(is.matrix(A)))   return(" A is not a matrix. Please provide a mtrix")
  if (!(is.matrix(b)))   return(" b is not a matrix. Please provide a mtrix")
  
  # Checking if the input matrix is 3x3 matrix
  
  if (!(isTRUE(all.equal(dim(A), c(3, 3))))) return("please provide 3x3 matrix")
  
  # Checking if the pivot element is 0.
  
  if (is.matrix(A)) if (A[1,1] == 0) return("First Pivot cannot be 0. Please provide anoher matrix")
  if (is.matrix(A)) if (A[2,2] == 0) return("Second Pivot cannot be 0. Please provide anoher matrix")
  if (is.matrix(A)) if (A[3,3] == 0) return("Third Pivot cannot be 0. Please provide anoher matrix")
   
  
  solve(A, b) 
           
}

Testing

# Tesing fo rbad matrices
A<- "Not a matrix"
b<- "Not a matrix"

solve_matrix(A,b)
## [1] " A is not a matrix. Please provide a mtrix"
## Testing for 3x3 matrix
A<- matrix(c(0,2))
b<- matrix(c(1, 2, 6))

solve_matrix(A,b)
## [1] "please provide 3x3 matrix"
## Testing if the first Pivot element is zero
A <- matrix(nrow = 3, ncol = 3, data = c(0, 1, 3, 2, -1, 5, -1, -2, 4), byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    0    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
b<- matrix(c(1, 2, 6))
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
solve_matrix(A,b)
## [1] "First Pivot cannot be 0. Please provide anoher matrix"
## Testing if the second Pivot element is zero
A <- matrix(nrow = 3, ncol = 3, data = c(1, 1, 3, 2, 0, 5, -1, -2, 4), byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2    0    5
## [3,]   -1   -2    4
b<- matrix(c(1, 2, 6))
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
solve_matrix(A,b)
## [1] "Second Pivot cannot be 0. Please provide anoher matrix"
## Testing if the third Pivot element is zero
A <- matrix(nrow = 3, ncol = 3, data = c(1, 1, 3, 2, -1, 5, -1, -2, 0), byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    0
b<- matrix(c(1, 2, 6))
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
solve_matrix(A,b)
## [1] "Third Pivot cannot be 0. Please provide anoher matrix"
## Successfull Test
A <- matrix(nrow = 3, ncol = 3, data = c(1, 1, 3, 2, -1, 5, -1, -2, 4), byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
b<- matrix(c(1, 2, 6))
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
solve_matrix(A,b)
##            [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,]  0.9545455

An alternative method is to use the built in function ginv()

solve_matrix_ginv <- function (A,b){
  
  
  # Checkin if the input values are matrices
  if (!(is.matrix(A)))   return(" A is not a matrix. Please provide a mtrix")
  if (!(is.matrix(b)))   return(" b is not a matrix. Please provide a mtrix")
  
  # Checking if the input matrix is 3x3 matrix
  
  if (!(isTRUE(all.equal(dim(A), c(3, 3))))) return("please provide 3x3 matrix")
  
  # Checking if the pivot element is 0.
  
  if (is.matrix(A)) if (A[1,1] == 0) return("First Pivot cannot be 0. Please provide anoher matrix")
  if (is.matrix(A)) if (A[2,2] == 0) return("Second Pivot cannot be 0. Please provide anoher matrix")
  if (is.matrix(A)) if (A[3,3] == 0) return("Third Pivot cannot be 0. Please provide anoher matrix")
  
  
  library(MASS)  
  ginv(A,0) %*% b 
  

}


## Successfull Test
A <- matrix(nrow = 3, ncol = 3, data = c(1, 1, 3, 2, -1, 5, -1, -2, 4), byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
b<- matrix(c(1, 2, 6))
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
solve_matrix_ginv(A,b)
##            [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,]  0.9545455