Problem set 1

knitr::include_graphics("C:\\Users\\sergioor\\Desktop\\Old PC\\CUNY\\DATA605\\pset1.jpg")

(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)
dp <- u %*% v
dp  
##      [,1]
## [1,] -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 compMATer science definition.

\(\sqrt { \sum _{ i=1 }^{ m }{ \left| \left[ u \right] _{ i } \right| ^{ 2 } } }\)

Length of u:

length_u <- sqrt(sum(u^2))
length_u
## [1] 0.7071068

Length of v:

length_v <- sqrt(sum(v^2))
length_v
## [1] 5

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

3*u - 2*v
## [1] -4.5  9.5

(4)What is the angle between u and v?

rad <- acos(dp / (length_u * length_v))
# in degrees
rad * 180/pi
##         [,1]
## [1,] 98.1301

Problem set 2

knitr::include_graphics("C:\\Users\\sergioor\\Desktop\\Old PC\\CUNY\\DATA605\\pset2.jpg")

knitr::include_graphics("C:\\Users\\sergioor\\Desktop\\Old PC\\CUNY\\DATA605\\pset2a.jpg")

Matrix A

A <- matrix(c(1,1,3,2,-1,5,-1,-2,4), 3, byrow=T)
b <- matrix(c(1,2,6))
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4

Vector b

b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6

Create augmented matrix

bind <- function(A,b){
    Augmented <- cbind(A,b)
}

Create triangle matrix

triangle <- function(Augmat){
    for (i in 2:nrow(Augmat)){
        for(j in 1:(i - 1)){
            Augmat[i,] <-  Augmat[i,] - (Augmat[j,] * (Augmat[i, j]/Augmat[j, j])) 
        }
    }
    Augmat
}

Back solve upper triangle matrix, return vector x

back_solve <- function(MAT){
  x <- c(NA*3)
  x[3] <- MAT[3,4] / MAT[3,3]
  x[2] <- (MAT[2,4] - MAT[2,3] * x [3]) / MAT[2,2]
  x[1] <- (MAT[1,4] - MAT[1,2] * x[2] - MAT[1,3] * x[3]) / MAT[1,1]
  x <- round(x, 2)
  x
}

Function to solve

## cal functions to produce x that solves Ax = b for 3*3 matrix w/ no zero pivots
solve_3X3_nozero_pivot <- function(A,b){
    Ab <- bind(A,b)
    Ab
    MAT <- triangle(Ab)
    MAT
    x <- back_solve(MAT)
    x
}

solve_3X3_nozero_pivot(A,b)
## [1] -1.55 -0.32  0.95