-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_loop_example_2.R
52 lines (33 loc) · 909 Bytes
/
for_loop_example_2.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
# --For Loop Example--
# Define an empty numeric vector s_n of size 25 using
# s_n <- vector("numeric", 25).
# Compute the the sum when n is equal to each
# integer from 1 to 25 using the function we defined
# in the previous exercise: compute_s_n
# Save the results in s_n
# Define a function and store it in `compute_s_n`
compute_s_n <- function(n){
x <- 1:n
sum(x^2)
}
# Create a vector for storing results
s_n <- vector("numeric", 25)
# write a for-loop to store the results in s_n
for(i in 1:25){
s_n[i] <- compute_s_n(i)
}
plot(1:25,s_n[1:25] ) #Plot XY
## Define the function
compute_s_n <- function(n){
x <- 1:n
sum(x^2)
}
# Define the vector of n
n <- 1:25
# Define the vector to store data
s_n <- vector("numeric", 25)
for(i in n){
s_n[i] <- compute_s_n(i)
}
# Check that s_n is identical to the formula given in the instructions.
identical(s_n, n*(n+1)*(2*n+1)/6)