## Problem Set 1:

Please notice that I will not be using any external library to do linear algebra for me. Everything I will use is in R Base and it’s well documented in R documentation. Also notice that I show 2 diffeent ways to run an answer. I comment one and I use the other. But both of them will work. Example - dot product of u and v, according to R documentation, can be expressed as u%*%v OR sum (u*v).

#### Calculate the dot product u.v where u = [0.5; 0.5] and v = [3;-4]

u.v = u1v1 + u2v2 = (0.5)*3 + (0.5)*(-4) = 1.5 - 2 = -0.5

u <- c(0.5, 0.5)
v <- c(3, -4)

#dot.product.uv <- u%*%v
dot.product.uv <- sum (u * v)
dot.product.uv
## [1] -0.5

#### What are the lengths of u and v?

len_u =sqrt(u.u) ; len_v = sqrt(v.v)

#len_u <- sqrt(u%*%u)
len_u <- norm(u, type="2")
len_u
## [1] 0.7071068
#len_v <- sqrt(v%*%v)
len_v <- norm(v, type="2")
len_v
## [1] 5

#### What is the linear combination: 3u - 2v ?

myCombination <- 3*u - 2*v

myCombination
## [1] -4.5  9.5

#### What is the angle between u and v?

The Math says:

Let teta be the angle between u and v

cos(teta) = (u.v)/(||u||*||v||) = (u.v)/(len_u * len_v)

and the code says:

teta <- acos(dot.product.uv / (len_u * len_v))

#we don't want radians, we usually visual things in Degrees. So let's convert this radian result into Degrees
teta_degree <- as.numeric(teta) * 180 / pi
teta_degree
## [1] 98.1301

## Problem Set 2:

#### Let’s create our function

the_Matrix_Solver <- function(A, b)
{
A <- cbind(A, b)
reducer <- A[2,1]/A[1,1]*(A[1,])
A[2,] = A[2,] - reducer

reducer = (A[3,1]/A[1,1]*(A[1,]))
A[3,] = A[3,] - reducer

reducer = (A[3,2]/A[2,2]*(A[2,]))
A[3,] = A[3,] - reducer

x3 <- (A[3,4]/A[3,3])
x2 = (A[2,4]-A[2,3]*x3)/A[2,2]
x1 = (A[1,4]-A[1,3]*x3 - A[1,2]*x2)/A[1,1]

return(c(x1, x2, x3))
}

#### let’s test it against our homework example and prove that the solution is: (−1.55, −0.32, 0.95)

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

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

the_Matrix_Solver(A, b)
## [1] -1.5454545 -0.3181818  0.9545455