Assignment 1

Problem 1

  1. \[ \\\overset{}{\rightarrow}u = [.5, .5] \\\overset{}{\rightarrow}v = [3, -4] \\\overset{}{\rightarrow}u \cdot \overset{}{\rightarrow}v = \overset{}{\rightarrow}u^{T} \cdot \overset{}{\rightarrow}v \\\begin{bmatrix}.5,.5 \end{bmatrix}\begin{bmatrix}3,-4 \end{bmatrix} \\=(.5)(3) + (.5)(-4) = 1.5 - 2 = -.5\]

  2. \[\\\overset{}{\rightarrow}||u|| = \sqrt{(.5^{2})(.5^{2 })} = \sqrt{.5} \\ \overset{}{\rightarrow}||v|| = \sqrt{(3^{2})(-4^{2 }))} = \sqrt{25} = 5\]

  3. \[3\overset{}{\rightarrow}u - 2\overset{}{\rightarrow}v \\=3[.5,.5] - 2[3,-4] \\=[]1.5,1.5 + [-6.,8] \\=[-4.5,9.5]\]

  4. \[=\cos\theta = \frac{u_{1}v_{1} + u_{2}v_{2} }{\sqrt{u_{1}^{2}+u_{2}^{2}}\sqrt{v_{1}^{2}+v_{2}^{2}}} \\=\frac{(.5)(3) + (.5)(-4)}{\sqrt{.5^{2}+ .5^{2}}\sqrt{3^{2}+-4^{2}}} \\=1.5-2/5\sqrt{5} \\=\frac{\sqrt{5}}{50}\]

Problem 2

attribution - reconfigured the last algorithm after solving by hand. Added backsub.
http://stackoverflow.com/questions/16044377/how-to-do-gaussian-elimination-in-r-do-not-use-solve
    gauss_elimination = function(M,b,diagonal=T){
      M <- cbind(M, b)
      ge_one_column = function(M,r=1,c=1){
        for (i in (r + 1):nrow(M)){
          L_table[i,c] <<-  M[i,c]/M[r,c]
          M[i,] = M[i,]-(M[i,c]/M[r,c])*M[r,]
        }
        return(M)
      }
      swap_row = function(M,r1,r2,c1=1,c2=ncol(M)){
        swap = M[r1,c1:c2]
        M[r1,c1:c2] = M[r2,c1:c2]
        M[r2,c1:c2] = swap
        return(M)
      }
      check_zeroes = function(M,r,c){
        if (M[r,c] != 0){ #If base element is non-zero
          return ("Row swap not required!")
        }else{ #If base element is zero
          changerow = 0 #set changerow var to zero
          for (i in r:nrow(M)){#evaluate rest of rows in that column
            if (M[i,c] != 0){
              changerow = i
              break
            }
          }
          if (changerow == 0){ #If base element & all elements below are zero
            return("Skip this column")
          }else{ #If base element is zero but at least one below it is not
            return(c("Row swap required!",changerow))
          }
        }
      }
      
      backsub=function(U,b)
      { 
        x <- c(0)
        n <- nrow(U)
        for (i in (n:1)){
          x[i] <- b[i]
          if (i < n) {
            for (j in ((i+1):n)){
              x[i] <- x[i] - U[i,j] * x[j]
            }
          }
          x[i] <- x[i]/U[i,i]
        }  
        return(cbind(x))
      }
      
      #Main
      currow <- 1
      M.alt <- diag(nrow(M))
      L_table <- diag(nrow(M))
      for (curcol in 1:ncol(M)){
        a <- check_zeroes(M,currow,curcol)
        if (a[1] == "Row swap required!"){
          M <- swap_row(M,as.numeric(a[2]),curcol)
          M.alt <- swap_row(M.alt,currow,as.numeric(a[2]))
          if (as.numeric(a[2]) != 1){
            L_table <- swap_row(L_table,currow,as.numeric(a[2]),1,curcol-1)
          }
        }
        if (a[1] == "Skip this column"){
          next()
        }
        M <- ge_one_column(M,currow,curcol)
        if(currow+1 == nrow(M)){
          break
        }
        currow <- currow+1
      }
      
      b <- backsub(M,M[,4])
      
      if (diagonal == FALSE){
        return(list("P" = as.fractions(M.alt),
                    "L" = as.fractions(L_table),
                    "U : The matrix after the elimination"=as.fractions(M)))
      }else{
        D = diag(nrow(M))
        diag(D) = diag(M)
        for (i in 1:nrow(D)){
          M[i,] = M[i,]/diag(D)[i]
        }
        return(list("P" = as.fractions(M.alt),
                    "L" = as.fractions(L_table),
                    "D" = as.fractions(D),
                    "U : The matrix after the elimination"=as.fractions(M),
                    "b : The result set" = b))
      }
    }
    A <- matrix (
      c(1,1,3,2,-1,5,-1,-2,4), 
      byrow = T, 
      nrow = 3, 
      ncol = 3
    ) 
    
    b <- c(1,2,6)
    
    gauss_elimination(A,b,T)
## $P
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1   
## 
## $L
##      [,1] [,2] [,3]
## [1,]   1    0    0 
## [2,]   2    1    0 
## [3,]  -1  1/3    1 
## 
## $D
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0   -3    0
## [3,]    0    0 22/3
## 
## $`U : The matrix after the elimination`
##                        b    
## [1,]     1     1     3     1
## [2,]     0     1   1/3     0
## [3,]     0     0     1 21/22
## 
## $`b : The result set`
##               x
## [1,] -1.5454545
## [2,] -0.3181818
## [3,]  0.9545455