为什么使用 numpy 的 trapz 两条曲线中较大的一条具有较低的综合值?

Why does the larger of two curves have the lower integrated value using trapz from numpy?

使用 numpy 中的 trapz 我得到以下信息:

lewis_result_outer = np.trapz(lewis_few_elements_outer_y, x=lewis_few_elements_outer_x)
lewis_result_inner = np.trapz(lewis_few_elements_inner_y, x=lewis_few_elements_inner_x)
lewis_result = lewis_result_inner - lewis_result_outer
lewis_result_outer : 99.02254450000001
lewis_result_inner : 98.324206
lewis_result: -0.6983385000000055

看我做的内外图结果明显是错误的,图码:

   figure(1)
    plt.plot(lewis_few_elements_outer_x,      # X-axis
             lewis_few_elements_outer_y,      # Y-axis
             's',                       # Marker type
             color = 'green',           # Marker color
             markersize=6,              # Marker size
             alpha=0.5,                 # Gennemsigtighed
             label='Lewis 40 element, outer')                 
    plt.plot(lewis_few_elements_inner_x,      # X-axis
             lewis_few_elements_inner_y,      # Y-axis
             's',                       # Marker type
             color = 'blue',            # Marker color
             markersize=6,              # Marker size
             alpha=0.5,                 # Gennemsigtighed
             label='Lewis 40 element, inner')
    plt.xlabel("r/R", size=20)          # X-label name
    plt.ylabel("Cp for int", size=20)   # Y-label name
    plt.legend()
    plt.show

内部应该比外部大。然而并不是,为什么?

我确实确保我使用的列表排序如下:

lewis_few_elements_outer_y
Out[251]: 
[100.66,99.867,99.79,99.676,99.57,99.504,99.445,99.405,99.366,99.34,99.334,99.387,99.527,99.743,99.921,100.068,100.178,100.268,100.322]

lewis_few_elements_outer_x
Out[252]: 
[0.004,0.015,0.04,0.076,0.125,0.174,0.243,0.312,0.38,0.46,0.541,0.618,0.692, 0.762,0.828,0.883,0.925,0.959,0.998]

lewis_few_elements_inner_y
Out[253]: 
[100.707,99.939,99.887,99.82,99.762,99.734,99.706,99.697,99.684,99.671,99.657,99.672,99.755,99.902,100.012,100.121,100.212,100.296]

lewis_few_elements_inner_x
Out[254]: 
[0.003,0.015,0.039,0.074,0.118,0.177,0.239,0.309,0.38,0.458,0.543,0.617,0.691,0.762,0.826,0.884,0.927,0.988]

偶然,但您可以简单地验证 trapz(y, x) 只是

(0.5 * (y[1:] + y[:-1]) * (x[1:] - x[:-1])).sum()

事实证明,问题是缩放和跨度问题之一。您的图表显示这些段非常不同,但请注意高度差异大约为 ~100 中的 ~0.4。这是一个图表,显示了您以更具代表性的比例添加的细分市场:

很明显,lewis_few_elements_outer_x 跨越的领域明显比 lewis_few_elements_inner_x 广泛。因此,无论函数顶部的微小差异看起来如何,最重要的是右边距。

>>> np.ptp(lewis_few_elements_outer_x)
0.994
>>> np.ptp(lewis_few_elements_inner_x)
0.985

即兴发挥,你用约 0.3% 的高度减少换取约 1% 的宽度增加,果然,差异大约为 0.7%,有利于更宽的数据集。

参考剧情代码:

l, = plt.plot(lewis_few_elements_inner_x, lewis_few_elements_inner_y, label='inner')
for x, y in zip(lewis_few_elements_inner_x, lewis_few_elements_inner_y):
    plt.plot([x, x], [y, 0], c=l.get_color())
l, = plt.plot(lewis_few_elements_outer_x, lewis_few_elements_outer_y, label='outer')
for x, y in zip(lewis_few_elements_outer_x, lewis_few_elements_outer_y):
    plt.plot([x, x], [y, 0], c=l.get_color())
plt.legend()