PROBLEM SET 1

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

u <- matrix(c(0.5, 0.5), nrow=2, ncol=2)
v <- matrix(c(3, -4), nrow=2, ncol=2)

dp <- u %*% v
dp[1]
## [1] -0.5

The dot product is -0.5.


2. What are the lengths of \(u\) and \(v\)?

u_length <- sqrt( u[1]^2 + u[2]^2 )
u_length
## [1] 0.7071068
v_length <- sqrt( v[1]^2 + v[2]^2 )
v_length
## [1] 5

The length of \(u\) is \(\sqrt{0.5}\), or \(\approx 0.71\).

The length of \(v\) is \(5\).


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

# Create a matrix with the product of `u` times 3
u_prod <- matrix(c( u[1]*3, u[2]*3 ), nrow=2, ncol=1)

# Create a matrix with the product of `v` times 2
v_prod <- matrix(c( v[1]*2, v[2]*2 ), nrow=2, ncol=1)

# Final matrix subtracting both products
uv <- matrix(c( u_prod[1]-v_prod[1], u_prod[2]-v_prod[2] ), nrow=2, ncol=1)
uv
##      [,1]
## [1,] -4.5
## [2,]  9.5

The linear combination is \([-4.5, 9.5]\)


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

uv_theta <- ( u %*% v ) / (u_length * v_length)
uv_theta <- uv_theta[1]
uv_theta
## [1] -0.1414214
acos(uv_theta)
## [1] 1.712693

The angle between the two vectors is approximately \(1.71\) degrees.




PROBLEM SET 2

A. Prompt

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.


B. Test System

Coefficient matrix A

A <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), nrow=3, ncol=3)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4

Constraint vector b

b <- matrix(c(1, 2, 6), nrow=3, ncol=1)
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6

The solution we expect to see for this system is \(x = [-1.55, -0.32, 0.95].\)


C. Gaussian Elimination Algorithm

gauss <- function(A, b) {
    
  # Create a temporary augmented matrix and reorder by the first column. This will set the first pivot value to the highest number in the first column.
    ATemp <- cbind(A, b)
    ATemp <- ATemp[order(ATemp[, 1], decreasing = TRUE), ]
    
    # Separate the now ordered matrix back into A and b
    A <- ATemp[c(1, 2, 3), c(1, 2, 3)]
    b <- ATemp[, 4]
    
    n <- nrow(A)
    
    for (k in 1:(n - 1)) {
        
      # If there is no pivot element in a column, advance to the next one.
        i_max <- max(abs(A[, k]))
        if (i_max == 0) {
            k <- k + 1
            
        } else {
          
            for (i in (k + 1):n) {
              
                # Set the multiplier for the row
                f <- A[i, k]/A[k, k]
                
                # Fill the values below the pivot element with zeros
                A[i, k] <- 0
                
                # Apply the elimination procedure to each element of A
                for (j in (k + 1):n) {
                  A[i, j] <- A[i, j] - (f * A[k, j])
                }
                # Apply the elimination procedure to each element of b
                b[i] <- b[i] - (f * b[k])
            }
            
        }
    }
    
    # Round each value to 3 significant figures, and augment A to b
    print(signif((cbind(A, b)), digits=3))
    
    
    # BACKWARDS SUBSTITUTION
    
    # Initialize a variable x
    x <- 0
    
    for (i in (n:1)) {
        # First, set each element in x to equal the constraints vector
        x[i] <- b[i]
        
        # For every value in x, apply the elimination procedure
        if (i < n) {
            for (j in ((i + 1):n)) {
                x[i] <- x[i] - ( A[i, j] * x[j] )
            }
        }
        
        # Then, apply this transformation
        x[i] <- x[i]/A[i, i]
    }
    
    # Round each value to 3 significant figures and bind them into one vector
    x <- signif(cbind(x), digits = 3)
    print(x)
    
}

gauss(A, b)
##                  b
## [1,] 2 -1.0 5.00 2
## [2,] 0  1.5 0.50 0
## [3,] 0  0.0 7.33 7
##           x
## [1,] -1.550
## [2,] -0.318
## [3,]  0.955


D. Results

The algorithm was successful. It returned the solution we expected to see for this system: \(x = [-1.550, -0.318, 0.955]\).


E. Sources

  1. YouTube: “Gauss algorithm to solve systems of linear equations, linear algebra, easy way, Part I (3x3)”

  2. Wikipedia: “Gaussian Elimination”

  3. StackOverflow: “How to do Gaussian elimination in R (do not use ‘solve’)”

  4. Gordon College: “Gaussian Elimination Algorithm - No Pivoting”