繁体   English   中英

在 R 中对双 Reimann 和进行硬编码

[英]Hard Coding a Double Reimann Sum in R

我正在尝试在 R 中为 function f(x,y) = x^5 * y^2 编写双 Reimann 总和,其中 0 < x < 2 和 4 < y < 6。这就是我目前所拥有的,但我很难找到正确的答案,因为这段代码输出了一个不正确的值。

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
f <- outer(x, y, function(x, y) x^5 * y^2)
ans <- sum(f)*xsize*ysize
ans
#> [1] 540.4438

尽管显然在这种情况下,您可以独立地进行 x 和 y 总和,而不必进行外积。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM