# Integrate Function in R (Example) | Using integrate() to Compute Integral

This tutorial illustrates how to integrate a user-defined function using integrate() in R.

Let’s do this…

## Constructing Example Function

The following manually defined function is used as basement for this R programming tutorial:

```my_function <- function(x) {    # Create own function
out <- 2 * x + x^2 + 5 * x^3
}```

## Example: Applying integrate() to User-Defined Function

This Example shows how to compute the integral of a function using the integrate() command of the R programming language. For this, we need to specify the name of our function, a lower limit, and an upper limit:

```integrate(my_function,          # Apply integrate in R
lower = 0,
upper = 10)
# 12933.33 with absolute error < 1.4e-10```

The RStudio console returns the result: 12933.33 with absolute error ＜ 1.4e-10.

## Video & Further Resources

I have recently published a video on my YouTube channel, which illustrates the examples of this tutorial. You can find the video below.

In addition, you might want to have a look at the other tutorials of my website. Please find some other articles below.

In this article you learned how to apply the integrate function in R. In case you have any further questions, don’t hesitate to let me know in the comments section.

Subscribe to the Statistics Globe Newsletter

• Gearoid
September 24, 2022 11:00 am

Thanks – how do you then extract the value of the integral (12933.33 in the example above) to use in further calculations?

• September 29, 2022 4:15 pm

Hey Gearoid,

Please have a look at the modified example code below:

```my_function ＜- function(x) {
out ＜- 2 * x + x^2 + 5 * x^3
}

x <- integrate(my_function,
lower = 0,
upper = 10)
x\$value
#  12933.33```

Regards,
Joachim

• Gearoid
September 29, 2022 4:24 pm

Thank you, Joachim.

• September 30, 2022 5:47 pm

Hi Gearoid,

Thank you for the kind comment, glad it helped!

Regards,
Joachim

• Sarv
October 16, 2022 11:42 am

Thank you so much, it helped me too

• November 7, 2022 10:52 am

Hi Sarv,

Thank you very much for the kind words, glad you liked our tutorial!

Regards,
Matthias

• Theo
November 16, 2022 6:20 pm

How would you make the function for unknown bounds/limits

• November 17, 2022 9:00 am

Hi Theo,

It is possible to assign Inf (i.e. infinite) to the lower and upper arguments.

Regards,
Joachim

• Vitomir
January 30, 2023 6:21 pm

Hello Joachim,

How can I construct the interval without the x value function inside? I only need an integral of 1/4 between 1 and 9.

Something like —> a <- integrate( 1/4, lower = 1, upper = 9)

Thanks,
Vitomir

• January 31, 2023 9:53 am

Hello Vitomir,

I am also not so familiar with the integrate function, but my quick search led me to this solution:

```my_function <- function(x) {    # Create own function
1/4
}

integrate(Vectorize(my_function),1,9)
# 2 with absolute error < 2.2e-14```

See the source here.

Regards,
Cansu