首页 > 解决方案 > 使用数值积分找到坐标环所包围的区域

问题描述

我正在尝试查找或编写一些代码,可以为我提供两行之间的区域,如果较低的行与较高的行交叉,则说明负区域。为此(无法发布图像:()。我已经编写了代码,但对答案不是 100% 有信心。我也尝试使用 np.trapz 的 numpy 函数。

def TrapzCn(n):

Uy = Upper(n)
Ly = Lower(n)
xc = Open(L[0])
Top = np.trapz(Uy,x=xc[:10])
Bottom = np.trapz(Ly,x=xc[:10])
return(Top + Bottom)

这是我的

def Cn(n):

Uy = Upper(n)
xc = Open(L[0])
Ly = Lower(n)
for i in range(len(Ly)):
  Ly[i] = float(Ly[i])
  Uy[i] = float(Uy[i])
A = 0
i = 0
while i+1 < len(Uy):
    A1 = (xc[i+1] - xc[i]) * ((Uy[i] + Uy[i+1])/2)
    A2 = (xc[i+1] - xc[i]) * ((Ly[i] + Ly[i+1])/2)
    if A1 > A2 and  Uy[i] < 0.0 :
        A += abs(A1) + abs(A2)
    elif A1 > A2 and ((Uy[i] + Uy[i+1] )/2)> 0:
        A += abs(A1) - abs(A2)
    elif A1 < A2 and A1 < 0.0 :
        A -= (abs(A2) - abs(A1))
    i += 1
return(A)

Upper 和 Lower 函数只是在解压缩我的数据。所以我的主要问题是,np.trapz 函数是否解释了曲线本身翻倍以及它们交叉后的负区域。如果没有,我的呢?

标签: pythonnumpyareanumerical-integration

解决方案


推荐阅读