根据浓度和空间值计算浓度梯度

时间:2013-04-04 20:59:18

标签: r

看起来很简单,但我在这里遇到了一些问题。我有浓度和位置(x)数据。我想从数据集中找到梯度(dc / dx)。

数据集可在https://www.dropbox.com/s/xgo3cx8i3xz27w3/concentration.csv

上找到

这里给出了一个样本数据:

       x      conc
1    0.0 1.0000000
2    0.5 1.0000000
3    1.0 1.0000000
4    1.5 1.0000000
5    2.0 1.0000000
6    2.5 1.0000000
7    3.0 1.0000000
8    3.5 1.0000000
9    4.0 1.0000000
10   4.5 1.0000000
11   5.0 1.0000000
12   5.5 1.0000000
13   6.0 1.0000000
14   6.5 1.0000000
15   7.0 1.0000000
16   7.5 1.0000000
17   8.0 1.0000000
18   8.5 1.0000000
19   9.0 1.0000000
20   9.5 1.0000000
21  10.0 1.0000000
22  10.5 1.0000000
23  11.0 1.0000000
24  11.5 1.0000000
25  12.0 1.0000000
26  12.5 1.0000000
27  13.0 1.0000000
28  13.5 1.0000000
29  14.0 1.0000000
30  14.5 1.0000000
31  15.0 1.0000000
32  15.5 1.0000000
33  16.0 1.0000000
34  16.5 1.0000000
35  17.0 1.0000000
36  17.5 1.0000000
37  18.0 1.0000000
38  18.5 0.9999999
39  19.0 0.9999999
40  19.5 0.9999999
41  20.0 0.9999999
42  20.5 0.9999999
43  21.0 0.9999999
44  21.5 0.9999999
45  22.0 0.9999999
46  22.5 0.9999999
47  23.0 0.9999998
48  23.5 0.9999998
49  24.0 0.9999998
50  24.5 0.9999998
51  25.0 0.9999998
52  25.5 0.9999997
53  26.0 0.9999997
54  26.5 0.9999997
55  27.0 0.9999996
56  27.5 0.9999996
57  28.0 0.9999995
58  28.5 0.9999995
59  29.0 0.9999994
60  29.5 0.9999994
61  30.0 0.9999993
62  30.5 0.9999992
63  31.0 0.9999991
64  31.5 0.9999990
65  32.0 0.9999989
66  32.5 0.9999988
67  33.0 0.9999987
68  33.5 0.9999986
69  34.0 0.9999984
70  34.5 0.9999982
71  35.0 0.9999981
72  35.5 0.9999979
73  36.0 0.9999976
74  36.5 0.9999974
75  37.0 0.9999971
76  37.5 0.9999969
77  38.0 0.9999965
78  38.5 0.9999962
79  39.0 0.9999958
80  39.5 0.9999954
81  40.0 0.9999950
82  40.5 0.9999945
83  41.0 0.9999940
84  41.5 0.9999934
85  42.0 0.9999928
86  42.5 0.9999921
87  43.0 0.9999913
88  43.5 0.9999905
89  44.0 0.9999896
90  44.5 0.9999887
91  45.0 0.9999876
92  45.5 0.9999865
93  46.0 0.9999853
94  46.5 0.9999839
95  47.0 0.9999825
96  47.5 0.9999809
97  48.0 0.9999792
98  48.5 0.9999773
99  49.0 0.9999754
100 49.5 0.9999732

可以使用以下公式找到渐变

dc / dx =(c2-c1)/(x2-x1)

所以,第一个值没有渐变,我需要使用上面的公式找到其余点的渐变。

感谢。

2 个答案:

答案 0 :(得分:2)

假设是data.frame,名为df

df$gradient <- c(NA, diff(df$c)/diff(df$x))

答案 1 :(得分:1)

中心有限差异 其中x是向量(浓度) res,点之间的距离(分辨率,在你的情况下为0.5) dx渐变

该方法将此公式用于极值之间的值

dx(n)=(x(n + 1)-x(n-1))/(res(n + 1) - res(n-1))

并使用前向方法计算极值

dx(n)=(x(n + 1)-x(n))/(res(x + 1)-res(x)) dx(m)=(x(m) - x(m-1))/ res(m) - res(m-1))

center_finite_diff <- function(x, res) {
    dx <- vector(mode="numeric",length=length(x))
    m <- end(x)
    dx[1] <- diff(x[1:2])/res
    dx[m[1]] <- diff(x[(m[1]-1):m[1]])/res
    dx[2:(m[1]-1)] <- diff(x, 2)/(2*res)
    return(dx)
}