-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcercise_04_week_04.R
98 lines (90 loc) · 1.55 KB
/
Excercise_04_week_04.R
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# exercise 1.1
f <- function(x){
x^2-x-1
}
fIterative <- function(x){
x <- 1 + 1/x
}
guess <- 1
root <- function(f , fIterative , guess){
x <- guess
i <- 1
for (i in 1:8){
x <- 1 + 1/x
}
x
}
root(f, fIterative , guess)
# exercise 1.2
x0 <- 0
x1 <- 1
tol <- 10^-10
while (abs(x0-x1) > tol){
x1 <- 1 + 1/x0
}
# exercise 1.3
# introducing an maxit = 30
j <- 1
x0 <- 0
x1 <- 1
tol <- 10^-10
while (abs(x0-x1) > tol){
x1 <- 1 + 1/x0
j <- j + 1
if (j > 30 ){
break
}
}
j;x1
# exercise 1.4
# exercise 2.1 here i is the index or position and
exp1 <- 0
m <- 100
for (i in 0:m){
exp1<- exp1 + sum(1/factorial(i))
}
exp1^3
exp(3)
# exercise 2.2
x <- 3
k_100 <- 0:100
exp2<- sum(1/factorial(k_100))^3 ; exp2
# exercise 2.3
x <- 3
k_5 <- 0:5
exp3 <- sum(1/factorial(k_5)); exp3
exp(3)
# comparing the two
appro_err <- abs(exp3 - exp(3)) ; appro_err
# exercise 2.4
m <- 3:20
exp_err <- c()
n <- length(m)
exp_err <- numeric(n)
j <- 1
for (i in seq_along(m)){
exp_err[j] <- exp_err[j] + abs((sum(1/factorial(m))^3-exp(3)))
}
exp_err
# exercise 2.5
m <- 5
tol <- 10^-10
err <- abs((sum(1/factorial(m))^3 - exp(3)))
while (err > tol) {
m <- m + 1
}
m # it does not stop , hence optimal m is infinity if that makes sense
# exercise 2.6
m <- 5
tol <- 10^-10
err <- abs((sum(1/factorial(m))^3 - exp(3)))
i <- 0
while (err > tol){
m <- m + 1
i <- i + 1
if (i >= 100){
cat("I'm outside")
break
}
}
i ; m # @100-th iteration and m = 105