Problem Set One

Answer

  1. Calculate the dot product u:v where

\[\vec{u} =\begin{bmatrix}0.5 \\ 0.5 \end{bmatrix}\] \[\vec{v} =\begin{bmatrix}3 \\ 4 \end{bmatrix}\] (1) Dot Product

w <- (u[1]*v[1]) + (u[2]*v[2])
w
## [1] -0.5
  1. Lengths
length_of_vector_u <-sqrt(sum(u^2))
length_of_vector_u
## [1] 0.7071068
length_of_vector_v <-sqrt(sum(v^2))
length_of_vector_v
## [1] 5
  1. Linear Combination
linear_combo <- (3*u) - (2*v)
linear_combo 
## [1] -4.5  9.5
  1. Angle between u and v is the inverse cosine (or arc cosine) of the dot production of the two vectors divided by the product of their magnitude.
angle <- acos(w/(length_of_vector_u*length_of_vector_v))
angle
## [1] 1.712693

Problem Set Two

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….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.

Answer

The function below solves a system of three equations by augmenting matrix A with vector b and jointly applying the Gauss Jordan elimination procedure. The function should return a tuple which provides a solution that can solve all three equations simulataneously.

There are four rules that this function has to follow:

  1. An equation is swapped with another
  2. An equation has both sides multiplied by a nonzero constant
  3. An equation is replaced by the sum of itself and a multiple of another
  4. Multiplying a row by 0 is not allowed.

In the code below, I print out the updated matrix after each application of the rules above to ensure that everything is working correctly.

solve_three_by_three_equations <- function(m, v){
    #These next two lines augment the matrix with the vector and removes the column names
    aug_matrix <- cbind(m,v)
    colnames(aug_matrix) <- NULL
    print('Below is the original augmented matrix:')
    print(aug_matrix)
    cat("\n\n")
    
    #Initialize the row and col variables to "1".
    col <- 1
    row <- 1
    
    #Check if the first coefficient in the first row, first column equals 1. If yes, proceed with the other rules.
    if (aug_matrix[row,col] == 1){
        
        #Apply Rules (2) and (3) multiply both sides of an equation by a nonzero constant and replaced by the sum of itself and a multiple of another
        if (aug_matrix[row+1,col] != 0){
            aug_matrix[row+1,] <- (-aug_matrix[row+1,col] * aug_matrix[row,]) + aug_matrix[row+1,]
            #print the updated augmented matrix
            print('The first step applied to the augmented matrix:')
            print(aug_matrix)
            cat("\n\n")
        }
        
        #Apply Rules (2) and (3) multiply both sides of an equation by a nonzero constant and replaced by the sum of itself and a multiple of another
        if (aug_matrix[row+2,col] !=0){
            aug_matrix[row+2,] <-  (-aug_matrix[row+2,col] * aug_matrix[row,]) + aug_matrix[row+2,]
            #print the updated augmented matrix
            print('The first column is now complete:')
            print(aug_matrix)
            cat("\n\n")
        }
        
        #Apply Rule (2) An equation has both sides multiplied by a nonzero constant in order to get a "1" in the second row, second column
        if (aug_matrix[row+1,col+1]!=0){
            aug_matrix[row+1,] <- (1/aug_matrix[row+1,col+1]) * aug_matrix[row+1,]
            #print the updated augmented matrix
            print('The second row and column is now complete:')
            print(aug_matrix)
            cat("\n\n")
        }
        
        #Apply Rules (2) and (3) multiply both sides of an equation by a nonzero constant and replaced by the sum of itself and a multiple of another
        if (aug_matrix[row+2,col+1] != 0){
            aug_matrix[row+2,] <- (-aug_matrix[row+2,col+1] * aug_matrix[row+1,]) + aug_matrix[row+2,]
            #print the updated augmented matrix
            print('The third row now has a 0 in its second column:')
            print(aug_matrix)
            cat("\n\n")
        }
        
        #For the final step, apply Rule (2) An equation has both sides multiplied by a nonzero constant in order to get a "1" in the third row, third column
        if (aug_matrix[row+2,col+2] !=0){
            aug_matrix[row+2,] <- (1/aug_matrix[row+2,col+2]) * aug_matrix[row+2,]
            #print the final Gauss matrix
            print('The final matrix:')
            print(aug_matrix)
            cat("\n\n")
        }
    }
    
    #Use backward population to find the values of x1, x2, and x3. 
    x3 <- aug_matrix[row+2,col+3]
    x2 <- aug_matrix[row+1,col+3] - (aug_matrix[row+1,col+2]* aug_matrix[row+2,col+3])
    x1 <- aug_matrix[row,col+3] - (aug_matrix[row+1,col+3] - (aug_matrix[row+1,col+2]* aug_matrix[row+2,col+3])) - (3*aug_matrix[row+2,col+3])
   
    #Create a solution vector and populate it
    solution <- c()
    solution[1] <- round(x1,2)
    solution[2] <- round(x2,2)
    solution[3] <- round(x3,2)
    
    #return the solution vector
    print("The final solution vector")
    print(x3)
    return (solution)
    }
m <-matrix(c(1,2,-1,1,-1,-2,3,5,4), ncol=3, nrow=3)
v <- c(1,2,6)
solution <- solve_three_by_three_equations(m,v)
## [1] "Below is the original augmented matrix:"
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    2   -1    5    2
## [3,]   -1   -2    4    6
## 
## 
## [1] "The first step applied to the augmented matrix:"
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    0   -3   -1    0
## [3,]   -1   -2    4    6
## 
## 
## [1] "The first column is now complete:"
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    1
## [2,]    0   -3   -1    0
## [3,]    0   -1    7    7
## 
## 
## [1] "The second row and column is now complete:"
##      [,1] [,2]      [,3] [,4]
## [1,]    1    1 3.0000000    1
## [2,]    0    1 0.3333333    0
## [3,]    0   -1 7.0000000    7
## 
## 
## [1] "The third row now has a 0 in its second column:"
##      [,1] [,2]      [,3] [,4]
## [1,]    1    1 3.0000000    1
## [2,]    0    1 0.3333333    0
## [3,]    0    0 7.3333333    7
## 
## 
## [1] "The final matrix:"
##      [,1] [,2]      [,3]      [,4]
## [1,]    1    1 3.0000000 1.0000000
## [2,]    0    1 0.3333333 0.0000000
## [3,]    0    0 1.0000000 0.9545455
## 
## 
## [1] "The final solution vector"
## [1] 0.9545455
solution
## [1] -1.55 -0.32  0.95

Reflections

Problem set one was straightforward, by the book. For problem Set two, I struggled a bit to understand the rules which I now understand. The function that I created, solve_three_by_three_equations, works on this particular matrix. I would have to add more code to get it work on different matrices. For example, I would have to add “else” statements for when a coefficient equals 0.