R Markdown

library(matlib)
library(MASS)

Prob Set 1

Prob (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)

sum(u*v)
## [1] -0.5

Prob (2)

What are the lengths of u and v

#length of u
sqrt(sum(u*u))
## [1] 0.7071068
#length of v
sqrt(sum(v*v))
## [1] 5

Prob (3)

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

x <- 3*u - 2*v

x
## [1] -4.5  9.5

Prob (4)

What is the angle between u and v

den <- sqrt(sum(u*u))*sqrt(sum(v*v))

theta <- acos(sum(u*v)/(sqrt(sum(u*u))*sqrt(sum(v*v))))

theta
## [1] 1.712693

Problem Set 2


However, I do not know how to create a generic code function. Perhaps, you show us in class?

attribution: http://stackoverflow.com/questions/16044377/how-to-do-gaussian-elimination-in-r-do-not-use-solve


A <- matrix(c(1,1,3,2,-1,5,-1,-2,4),byrow=T,nrow=3,ncol=3)
b <- matrix(c(1,2,6),nrow=3,ncol=1)
p <- nrow(A)
(Umat <- cbind(A,b))
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    2   -1    5    2
## [3,]   -1   -2    4    6
#Pivot: The first non-zero element in the row being evaluated
Umat[1,] <- Umat[1,]/Umat[1,1]

i <- 2
while (i < p+1) {
 j <- i
 while (j < p+1) {
  Umat[j, ] <- Umat[j, ] - Umat[i-1, ] * Umat[j, i-1]
  j <- j+1
 }
 
 
# Check to see if 2nd and 3rd pivot of Augmented Matrix A is 1 or not
# If not, make it to 1 by dividing by its own coeficient
 while (Umat[i,i] == 0) {
 Umat <- rbind(Umat[-i,],Umat[i,])#this checks if its indeed already zero, if so, row bind it together and move on
 }
 Umat[i,] <- Umat[i,]/Umat[i,i]# this makes it 1 by dividing its own coeficient
 i <- i+1
}

#Making off-pivot coeficient to zero
for (i in p:2){
 for (j in i:2-1) {
  Umat[j, ] <- Umat[j, ] - Umat[i, ] * Umat[j, i]
 }
}

Umat
##      [,1] [,2] [,3]       [,4]
## [1,]    1    0    0 -1.5454545
## [2,]    0    1    0 -0.3181818
## [3,]    0    0    1  0.9545455