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

 

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

Table of contents:

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.

 

14 Comments. Leave new

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

    Reply
    • 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
      # [1] 12933.33

      Regards,
      Joachim

      Reply
  • Thank you, Joachim.

    Reply
  • Thank you so much, it helped me too

    Reply
  • How would you make the function for unknown bounds/limits

    Reply
  • 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

    Reply
    • 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

      Reply
  • shivshankar nila
    July 2, 2024 9:39 am

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

    result<-integrate(my_function, # Apply integrate in R
    lower = 0,

    upper = 10,z=z)
    result$value

    How do we integrate this kind of integration? Here z is variable and taking value z.

    Reply
    • Hey,

      Are you looking for something like this?

      my_function <- function(x, z) {
        out <- 2 * x + x^2 + 5 * x^3 + z
        return(out)
      }
       
      z <- 1 # Example value for z
      result <- integrate(function(x) my_function(x, z), lower = 0, upper = 10)
      result$value

      Regards,
      Joachim

      Reply
  • shivshankar nila
    July 2, 2024 5:07 pm

    Thank you for your reply. The aim is to do double integration, as in the sample example below.
    First, integrate the function f(x,z)=xz with respect to x from x=z to x=10.
    Then, integrate the resulting expression with respect to z from z=0 to z=10.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top