3

I am trying to convert the below integral implementation in Mathematica into R syntax:

xprime = 0.5;
yprime = 3;
dimx = 20/1000;
dimy = 20/1000;

 NIntegrate[Boole[Abs[x - xprime] < dimx/2 && Abs[y - yprime] < dimy/2] 1/(20 Pi Sqrt[x^2 + y^2]),
   {x, y} \[Element] Region[Disk[{0, 0}, 10]]]

My code so far is below. It runs without producing an error but is not actually implementing the above integral. I think I'm getting stuck on restricting the set to the disk region.

library(calculus)

# Parameters
xprime <- 0.5
yprime <- 3
dimx <- 20/1000
dimy <- 20/1000
    
# Function
pb_function5km <- function(x, y) {      
    result <- ifelse(sqrt(x^2 + y^2) <= 5, 1/(10 * pi * sqrt(x^2+y^2)), 0)
    return(result)
    }

# Integrate
integral(pb_function5km, 
         list (x = c(xprime+dimx/2, xprime-dimx/2), 
         y = c(yprime+dimy/2, yprime-dimy/2)))

Thanks in advance for any help!

socialscientist
  • 3,759
  • 5
  • 23
  • 58
  • 1
    What have you tried so far? – socialscientist Aug 04 '22 at 16:56
  • Honestly, very little. I started to look at the integrate function and got very lost. – J. W. Rozelle Aug 04 '22 at 17:06
  • What did you not understand? You need to know what integral you are trying to solve then how to solve an integral in R. There are lots of examples of this e.g. https://community.rstudio.com/t/integrating-functions-in-r/118230/3 – socialscientist Aug 04 '22 at 17:12
  • So, where I'm getting stuck is integrating over the geometric region of the disk. I've updated my question a bit to reflect this. – J. W. Rozelle Aug 04 '22 at 19:08
  • Much more informative and likely to get an answer. What do you expect from the output of your code? In other words, what is it NOT doing now? Many R users don't know Mathematica, but could help you anyway if you had the actual mathematical notation for the integral. A tip as well: you can use ```r foo ``` to indicate you have R code and SO will implement syntax highlighting and you won't need to indent your code if you don't want. Makes your R code even easier to read. I edited your question with it...and added the library directly into the code. – socialscientist Aug 04 '22 at 22:12
  • Amazing, appreciate the help! I'll review the output I get and add more details re: what it's not doing now and work on the mathematical notation here. – J. W. Rozelle Aug 04 '22 at 23:15

0 Answers0