-1

I've been trying to wrap my head around this problem but I am unfortunately quite new to both integrals and R. Can anyone help me figure out what I might be doing wrong here? The problem is as follows: Question 3

Here's my code so far:

## Question 3
rm(list=ls())

## a)
X<-(2*(1:20))
X

## b)
Y<-rep(0,20)
Y

## c)
for (k in 1:20){
  Y[k] <- k
  if (k < 12){
    Y[k]<-cos(3*k)
  } else if (k >= 12) {
    integral <- function(t) sqrt(t)
    Y[k]<-integrate(integral, lower = 0, upper = k)
  }
}
Y

Here's my output when I run the code:

Warning messages:
1: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
2: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
3: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
4: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
5: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
6: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
7: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
8: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
9: In Y[k] <- integrate(integral, lower = 0, upper = k) :
  number of items to replace is not a multiple of replacement length
> Y
[[1]]
[1] -0.9899925

[[2]]
[1] 0.9601703

[[3]]
[1] -0.9111303

[[4]]
[1] 0.843854

[[5]]
[1] -0.7596879

[[6]]
[1] 0.6603167

[[7]]
[1] -0.5477293

[[8]]
[1] 0.424179

[[9]]
[1] -0.2921388

[[10]]
[1] 0.1542514

[[11]]
[1] -0.01327675

[[12]]
[1] 27.71282

[[13]]
[1] 31.24811

[[14]]
[1] 34.92214

[[15]]
[1] 38.72984

[[16]]
[1] 42.66667

[[17]]
[1] 46.72854

[[18]]
[1] 50.91169

[[19]]
[1] 55.21273

[[20]]
[1] 59.62849

My values for b) don't seem correct when I use a calculator to check the results and I have no idea why I am receiving the warning for c). I am also unfamiliar with integrals so I have no idea what kind of values I should expect for that problem. Thank you for any help you can provide!

  • You might find it helpful to use a simple expression for the integral of t^(1/2) evaluated at the appropriate limits. The problem does not specifically say that you must use R code to evaluate the integral. – Robert Sep 11 '16 at 19:20
  • Thank you I appreciate the help! – Nick Blaze Sep 13 '16 at 09:01

1 Answers1

0

The problem is, that integrate(...)is giving you a list as result and you want to replace this in a vector. That´s the error.

integral <- function(t) sqrt(t)
result <- integrate(integral, lower = 0, upper = 1)
is(result)
# [1] "integrate" "oldClass" 

result$value
# [1] 0.6666667

Here the solution for your problem. Your second line was not necessary.

for (k in 1:20){
  if (k < 12){
    Y[k]<-cos(3*k)
  } else if (k >= 12) {
    integral <- function(t) sqrt(t)
    result <- integrate(integral, lower = 0, upper = k)
    Y[k] <- result$value
  }
}
J_F
  • 9,956
  • 2
  • 31
  • 55