Problem set 1

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

# Dot product of vectors in 1st method
library("geometry")
u <- c(0.5,0.5)
v <- c(3,-4)
dotproduct <-  dot(u,v,d = NULL)
print(dotproduct)
## [1] -0.5
### Dotprodcut of vectors in 2nd method
u <- matrix(c(0.5,0.5), ncol=1)
v <- matrix(c(3,-4), ncol=1)
u<- c(0.5,0.5)
v<- c(3,-4)
dot_prod <- u %*% v
dot_prod
##      [,1]
## [1,] -0.5

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

# Finding length of the vectors
len_u <- sqrt(sum(u*u))
len_v <- sqrt(sum(v*v))
print(len_u)
## [1] 0.7071068
print(len_v)
## [1] 5

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

# Linearcombination of 3u-2v
u <- c(0.5,0.5)
v <- c(3,-4)
lin_comb <- 3*u - 2*v
lin_comb
## [1] -4.5  9.5

(4) What is the angle between u and v

# Angle between the vectors
angle_uv <- acos(dotproduct/(len_u*len_v))
angle_uv
## [1] 1.712693
angle_in_degrees <- angle_uv*180/pi
angle_in_degrees
## [1] 98.1301

Problem set 2

Please test it with the system below and it should produce a solution x = [−1.55, −0.32, 0.95]

# Solving matrix
m <- matrix(c(1,2,-1,1,-1,-2,3,5,4),nrow = 3, ncol = 3, byrow = FALSE)
m
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
b <- matrix(c(1,2,6), ncol = 1)
b
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
result <- solve(m,b)
round(result, digits = 2)  
##       [,1]
## [1,] -1.55
## [2,] -0.32
## [3,]  0.95

Including Plots

You can also embed plots, for example: