Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

area - Hard Coding a Double Reimann Sum in R

I am trying to code up a double Reimann sum in R for the function f(x,y) = x^5 * y^2 where 0 < x < 2 and 4 < y < 6. This is what I have so far, but I am having a hard time finding the correct answer since this code outputs an incorrect value.

a <- 0 # lower bound for x
b <- 2 # upper bound for x
c <- 4 # lower bound for y
d <- 6 # upper bound for y
xsize <- (b-a)/1000 # step size in x-direction
ysize <- (d-c)/1000 # step size in y-direction
x <- seq(a + (xsize/2), b - (xsize/2), xsize)
y <- seq(c + (ysize/2), d - (ysize/2), ysize)

f <- x^5 * y^2

ans <- sum(f*xsize*ysize)
ans
question from:https://stackoverflow.com/questions/65830100/hard-coding-a-double-reimann-sum-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
f <- outer(x, y, function(x, y) x^5 * y^2)
ans <- sum(f)*xsize*ysize
ans
#> [1] 540.4438

Although obviously in this case you could do the x and y sums independently and not have to do the outer product.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...