2
votes

I need to find evaluate the following function, including an integral, in R: Probability density functions involving multiple variables, where u = t - y.

The problem I'm running into is that while the input variables of the function as a whole are x and t, the integral needs to be evaluated over the variable u = t - y. The functions f and m' both return values, but I don't know how to make it so that R evaluates the integral over this u rather than x or T.

I currently have the following, but this doesn't return the values I'm supposed to be getting, so I'm wondering if I did it properly?

Thank you in advance!

a = 3
b = 10
T = 2.6

mprime = function(x){
  return (1/x)
}

f = function(x){
  if (a <= x & x <= b){
    return (1/(b-a))
  }
  else{
    return (0)
  }
}

toIntegrate = function(u){
  return (f(u + x)*mprime(T-u))
}

solution = function (x, T){
  return (f(T + x)) + (integrate(toIntegrate(T-y), 0, T))
}

solution(5,T)
1
What is y? I don't see it defined anywhere.Rohit

1 Answers

0
votes

There are a few errors in your code:

  1. f and other functions you'll be using in your integration need to be vectorised, i.e. it should take a vector as an input and return a vector as an output.
  2. toIntegrate uses x which is neither a global variable nor an argument.
  3. return is a function, so only the expression between parentheses are returned. As a result, your integral would not be evaluated because your function would return f(T+x)
  4. The first argument to integrate should be toIntegrate, not toIntegrate(T-y)
  5. mprime will return infinity for u=t, so the limits may need to be adjusted.
a = 3
b = 10
t = 2.6

mprime = function(x){
  return (1/x)
}

f = function(x){
  ifelse(a <= x & x <= b,1/(b-a),0)
}

toIntegrate = function(u,x,t){
  return (f(u + x)*mprime(t-u))
}

solution = function (x, t){
  return(f(t + x) + integrate(toIntegrate, 0, t,x=x,t=t,stop.on.error = F)$value)
}

solution(5,T)