简体   繁体   English

如何将梯形积分方法调整为自定义零点?

[英]How to adjust trapezoidal integration method to a custom zero point?

I want to calculate the integral of a sequence vector.我想计算一个序列向量的积分。 Since there's no function available, I use the trapezoidal method 1 .由于没有可用的函数,我使用梯形方法1

iglTzm <- function(x, y) sum(diff(x) * (head(y, -1) + tail(y, -1))) / 2

The first element of the sequence should be the zero point, so the principle is: if the values of the sequence are predominantly below the first value, the integral should be negative, otherwise positive, or 0.序列的第一个元素应该是零点,所以原则是:如果序列的值主要低于第一个值,则积分应为负,否则为正,或为 0。

Consider matrix m1 :考虑矩阵m1

     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    6    7    8    8    6    8   10
[2,]    9    9    8    9    9    8    9
[3,]    9   10   10    9    9    9    9
[4,]    9    8    8    8    6    8    9
[5,]   10   10   10    9   10    8    0
[6,]    9    8    9   10    9    9    9

Integration with these raw values will most likely lead to inconsistent values:与这些原始值的集成很可能会导致不一致的值:

> setNames(apply(m1, 1, iglTzm, 0:6), 1:6)
  1   2   3   4   5   6 
 15   2  -2   7 -52   0 

So I adjust the sequences (rows) on their first value (column 1), in order to set the right signs, and get matrix m2 :所以我在它们的第一个值(第 1 列)上调整序列(行),以设置正确的符号,并得到矩阵m2

     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    0    1    2    2    0    2    4
[2,]    0    0   -1    0    0   -1    0
[3,]    0    1    1    0    0    0    0
[4,]    0   -1   -1   -1   -3   -1    0
[5,]    0    0    0   -1    0   -2  -10
[6,]    0   -1    0    1    0    0    0

Logically that doesn't change anything about the values iglTzm() throws, because the diff() is the same:从逻辑上讲,这不会改变iglTzm()抛出的任何值,因为diff()是相同的:

> setNames(apply(m2, 1, iglTzm, 0:6), 1:6)
  1   2   3   4   5   6 
 15   2  -2   7 -52   0 

Anyway, because I can't simply scale or invert it, I haven't had a brilliant idea yet how to adapt the function to get the right signs, which are assumingly:无论如何,因为我不能简单地缩放或反转它,所以我还没有一个绝妙的主意如何调整函数以获得正确的符号,假设是:

#  1   2   3   4   5   6 
# 15  -2   2  -7 -52   0

Does anyone know how to adapt iglTzm() to get the integrals with the correct sign?有谁知道如何调整iglTzm()以获得具有正确符号的积分?

The plot of m2 should illustrate the principle a bit more: m2的图应该更多地说明原理:

在此处输入图片说明


data数据

m1 <- matrix(c(6, 7, 8, 8, 6, 8, 10,
                9, 9, 8, 9, 9, 8, 9,
                9, 10, 10, 9, 9, 9, 9,
                9, 8, 8, 8, 6, 8, 9, 
                10, 10, 10, 9, 10, 8, 0, 
                9, 8, 9, 10, 9, 9, 9), 6, byrow=TRUE)

m2 <- t(apply(m1, 1, function(x) scale(x, center=x[1], scale=FALSE)))

# plot
par(mfrow=c(2, 3))
lapply(1:nrow(m2), function(x) {
  plot(m2[x, ], type="l", main=x)
  abline(h=m2[x, 1], col="red", lty=2)
})

Firstly, there is another small but more important issue, although after fixing it your question would still remain valid.首先,还有另一个小但更重要的问题,尽管在修复它之后您的问题仍然有效。 What I mean is that the order of x and y as arguments of your function should be reversed due to how you use the function in apply .我的意思是,由于您在apply使用函数的方式,作为函数参数的xy的顺序应该颠倒。

But that is not enough and now we come back to your question.但这还不够,现在我们回到你的问题。 For that, let us recall the usual integration: ʃf(x)dx (with limits from a to b) would integrate the area below f, which is what your function already successfully does.为此,让我们回忆一下通常的积分: ʃf(x)dx(限制从 a 到 b)将积分 f 下方的区域,这就是您的函数已经成功执行的操作。 Now what you want is to adjust its level.现在你想要的是调整它的水平。 But if we integrate from a to b, that is the same as ʃ(f(x)-f(a))dx = ʃf(x)dx - (ba)f(a), which leads to但是如果我们从 a 积分到 b,那就和 ʃ(f(x)-f(a))dx = ʃf(x)dx - (ba)f(a) 一样,这导致

iglTzm <- function(y, x) sum(diff(x) * (head(y, -1) + tail(y, -1))) / 2 - y[1] * (max(x) - min(x))
setNames(apply(m1, 1, iglTzm, 0:6), 1:6)
#  1  2  3  4  5  6 
#  9 -2  2 -7 -8  0 

It happens that only two absolute values are different from the version where x and y are reversed.恰好只有两个绝对值与xy颠倒的版本不同。 Bet let's look at the first function: should it be 9 or 15?打赌让我们看看第一个函数:它应该是 9 还是 15? We have that 2*2/2 + 1*2 + 1*2/2 + 2*4/2 = 9, so indeed we want to reverse x and y .我们有 2*2/2 + 1*2 + 1*2/2 + 2*4/2 = 9,所以我们确实想要反转xy

Another way to write the function would be编写函数的另一种方法是

iglTzm <- function(y, x) sum(diff(x) * (head(y - y[1], -1) + tail(y - y[1], -1))) / 2
setNames(apply(m1, 1, iglTzm, 0:6), 1:6)
#  1  2  3  4  5  6 
#  9 -2  2 -7 -8  0 

Edit : by reversing I only meant the order in the function definition or how you use it in apply ;编辑:通过反转我只是指函数定义中的顺序或您如何在apply使用它; the function itself in terms of y (function values) and x (grid values) is fine.就 y(函数值)和 x(网格值)而言,函数本身很好。

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

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