CUNY DATA 605 - Week 1 Homework

Zachary Herold

February 3, 2019


Problem set 1

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

## [1] 3.5

Could also have used:
* t(u) %% v
crossprod(u,v)

(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 computer science definition.

## [1] 0.7071068

Could also have used:
* norm(u, type=“2”)

## [1] 5

Could also have used:
* norm(v, type=“2”)

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

## [1] 7.5 9.5

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

Making use of the formula:

## [1] "The angle between the vectors is: 8.13 degrees"

Problem set 2

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.

…The approach that you should employ is to construct an Upper Triangular Matrix and then back-substitute to get the solution…

First, I create a function to decompose the A matrix, verifying at the end that AX = B.

Next, I create a second function to imput the constraint vector.

Finally, I solve with the test matrices provided.

## [1] "Matrix A"
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
## [1] ""
## [1] "Matrix U"
##      [,1] [,2]      [,3]
## [1,]    1    1  3.000000
## [2,]    0   -3 -1.000000
## [3,]    0    0  7.333333
## [1] ""
## [1] "Matrix L"
##      [,1]      [,2] [,3]
## [1,]    1 0.0000000    0
## [2,]    2 1.0000000    0
## [3,]   -1 0.3333333    1
## [1] ""
## [1] "Matrix Decomposition LU"
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
## [1] ""
## [1] "The original matrix and multiplication of decomposed LU matrices is the same: TRUE"
## [1] -1.55 -0.32  0.95

The result is confirmed.