如何计算 python 中两个二维轨迹之间的面积

问题描述 投票:0回答:0

我必须计算两个 2D 离散轨迹(具有相同的长度)之间的面积,并且这两个轨迹之一不是单调的(对于 x 和 y)。

这是我拥有的两条轨迹的示例:

我尝试使用 np.trapeiz() 函数,但我不知道它是否足以正确计算这两条曲线之间的面积

这是我使用的代码:

# traj1 and traj2 are the x,y coordinates of the two trajectories that have the same lenght.

# Generate a set of x-coordinates that covers the range of both curves
x = np.union1d(traj1[:,0], traj2[:,0])

# Interpolate the y-coordinates
f = interp1d(traj1[:,0], traj1[:,1], kind='linear', bounds_error=False, fill_value=0)
g = interp1d(traj2[:,0], traj2[:,1], kind='linear', bounds_error=False, fill_value=0)
f_values = f(x)
g_values = g(x)

# Compute the area
area = np.trapz(f_values - g_values, x)

python numpy area
© www.soinside.com 2019 - 2024. All rights reserved.