-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_4.R
More file actions
33 lines (25 loc) · 660 Bytes
/
4_4.R
File metadata and controls
33 lines (25 loc) · 660 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#' ###
#' APTS 4. "Some exercises"
#' ###
rm(list=ls())
# Question 4 --------------------------------------------------------------
# a) ----
# Ax=y
?solve
set.seed(0)
n <- 1000
A <- matrix(runif(n*n), n, n)
x.true <- runif(n)
y <- A%*%x.true
# b) ----
A.solve <- solve(A)
system.time(x1 <- A.solve %*% y) # quick (0s)
# mean absolute difference
mean(abs(x1-x.true)) # 2.95e-11
# c) ----
system.time(x2 <- solve(A,y)) # bit longer (0.15s)
# mean absolute difference
mean(abs(x2-x.true)) # 1.35e-12, factor 10 more accurate.
# d) ----
#' There is a small 'gain' in speed calculating the inverse first,
#' for loss of accuracy of appx. magnitude 3e-11.