ASSIGNMENT 1

IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - FALL 2014

1. Problem set 1

You can think of vectors representing many dimensions of related information. For instance, Netflix might store all the ratings a user gives to movies in a vector. This is clearly a vector of very large dimensions (in the millions) and very sparse as the user might have rated only a few movies. Similarly, Amazon might store the items purchased by a user in a vector, with each slot or dimension representing a unique product and the value of the slot, the number of such items the user bought. One task that is frequently done in these settings is to nd similarities between users. And, we can use dot-product between vectors to do just that. As you know, the dot-product is proportional to the length of two vectors and to the angle between them. In fact, the dot-product between two vectors, normalized by their lengths is called as the cosine distance and is frequently used in recommendation engines.

(1) Calculate the dot product u.v where u = [0.5; 0.5] and v = [3;-4]

u <- c(0.5, 0.5)
v <- c(3, -4)
cat("Is u a vector:",  is.vector(u))
## Is u a vector: TRUE
cat("Is v a vector:",  is.vector(v))
## Is v a vector: TRUE
cat("Product of u and v: u.v=",u*v)
## Product of u and v: u.v= 1.5 -2
cat("Dot product of u and v: uxv",u%*%v)
## Dot product of u and v: uxv -0.5

(2) What are the lengths of u and v? Please note that the mathematical notion of the length of a vector is not the same as a computer science definition.

cat("Length of vector u:",  length(u))
## Length of vector u: 2
cat("Mathematical Length of vector u:",  sqrt(sum(u^2)))
## Mathematical Length of vector u: 0.7071068
cat("Length of vector v:",  length(v))
## Length of vector v: 2
cat("Mathematical Length of vector v:",  sqrt(sum(v^2)))
## Mathematical Length of vector v: 5

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

cat("Linear Combination:",  (3*u)-(2*v))
## Linear Combination: -4.5 9.5

(4) What is the angle between u and v

The cosine of the angle between the two vectors is defined as follows: \[ cos\theta = \frac{u???v}{||u||???||v||} \] \[ \theta=cos^{-1}(\frac{u???v}{||u||???||v||}) \]

dot_product <- u%*%v
length_of_u <- sqrt(sum(u^2))
length_of_v <- sqrt(sum(v^2))
radian <- acos(dot_product / (length_of_u * length_of_v))
angle <- radian * 180/pi

print(paste("The angle between u and v",round(angle,4)))
## [1] "The angle between u and v 98.1301"
#plot(u,v)
plot(u, v, xlab="x-axis -> u", ylab="y-axis -> v", main="Problem Set 1", ylim=c(-10,10), xlim=c(0,10), pch=15, col="blue")

library(OceanView)
vectorplot(u, v, x = 0, y = 0, NAcol = "white")

mag <- function(x) sqrt(sum(x^2))

plot(c(-10,10), c(-10,10), xlab="x-axis -> u", ylab="y-axis -> v", main="Plot of vectors u and v")

arrows(0,0, mag(u),mag(v),lwd=4,col="red")
arrows(-2,1, mag(u),mag(v),lwd=4,col="blue")


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 function solve to 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 the Gauss Jordan elimination procedure.

Please test it with the system below and it should produce a solution x = [-1.55,-0.32,0.95]

\[ \left[\begin{array}{rrr}1 & 1 & 3 \\2 & -1 & 5 \\-1 & -2 & 4\end{array}\right] \left[\begin{array}{rrr}x_1 \\x_2 \\x_3\end{array}\right] = \left[\begin{array}{rrr}1 \\2 \\6\end{array}\right] \]

Solution

solve_equation <- function(a, b) {
  colnames(a) <- c("x1", "x2", "x3")
  a <- cbind(a, b)
  pivot <- a[1, 1]
  multi <- a[2, 1]/pivot
  row1 <- a[1,]
  row2 <- a[2,]
  a[2,] <- row2 - row1 * multi
  
  # Run 2 or zero out position 3, 1
  pivot <- a[1, 1]
  multi <- a[3, 1]/pivot
  row1 <- a[1,]
  row3 <- a[3,]
  a[3,] <- row3 - row1 * multi
  
  # Run 3 or zero out position 3, 2
  pivot <- a[2, 2]
  multi <- a[3, 2]/pivot
  row2 <- a[2,]
  row3 <- a[3,]
  a[3,] <- row3 - row2 * multi
  
  # Solution
  x3 <- a[3, 4] / a[3, 3]
  x2 <- (a[2, 4] - a[2, 3] * x3) / a[2, 2]
  x1 <- (a[1, 4] - a[1, 3] * x3 - a[1, 2] * x2) / a[1, 1]
  x <- c(x1, x2, x3)
  return(round(x, 2))
}

vectorA <- c(1, 2, -1, 1, -1, -2, 3, 5, 4)
constantB <- c(1, 2, 6)
FinalValues <- matrix(vectorA, 3, 3)

solve_equation(FinalValues, constantB)
##     b     b     b 
## -1.55 -0.32  0.95
#Setting matrix for linear equation
A <- matrix(c(1,2,-1, 1, -1, -2,3,5,4), nrow=3, ncol=3, byrow=T)
b <- c(1,2,6)
showEqn(A, b)
## 1*x1 + 2*x2 - 1*x3  =  1 
## 1*x1 - 1*x2 - 2*x3  =  2 
## 3*x1 + 5*x2 + 4*x3  =  6

Using Echelon function:

echelon(A, b)
##      [,1] [,2] [,3]       [,4]
## [1,]    1    0    0  2.2727273
## [2,]    0    1    0 -0.4545455
## [3,]    0    0    1  0.3636364

Using Solve function:

Solve(A, b, fractions=FALSE)
## x1      =   2.27272727 
##   x2    =  -0.45454545 
##     x3  =   0.36363636
rref(A)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1