Search code examples
roptimizationintegrate

how to integrate of a function with multiple parameters w.r.t one of them in R?


I have a 2 variable function in R which I want to integrate w.r.t one of them and then optimize it w.r.t the other one. How can I do this?


Solution

  • You do not provide an example of the sort of function that you would like to integrate and then optimize, so I will use a somewhat arbitrary choice as an example.

    f(y) = ∫0y (y-1) x2 dx

    You can write this function in R using the integrate function and then use optimize to find a minimum over some specified range.

    DefInt = function(y) { integrate(function(x) { (y-1)*x*x },0,y)$value }
    optimize(DefInt, c(-2,2))
    $minimum
    [1] 0.7499972
    $objective
    [1] -0.03515625