Python 多列表上带数值积分的Scipy

Python 多列表上带数值积分的Scipy,python,python-3.x,scipy,numerical-integration,Python,Python 3.x,Scipy,Numerical Integration,我有一个带有跨距步长和相应旋转值的图。我需要对每一步进行数值积分以得到斜率值。我想知道,既然scipy集成中已经有内置函数,比如梯形规则或辛普森规则。在没有任何附加功能的情况下,如何在两个数组或数据列表上实现这一点 预期结果: result = [x0, x1, .........xn] 按照上述建议 import scipy fraction_of_span = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] rotation = [

我有一个带有跨距步长和相应旋转值的图。我需要对每一步进行数值积分以得到斜率值。我想知道,既然scipy集成中已经有内置函数,比如梯形规则或辛普森规则。在没有任何附加功能的情况下,如何在两个数组或数据列表上实现这一点

预期结果:

result = [x0, x1, .........xn]
按照上述建议

import scipy
fraction_of_span = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
rotation = [0.33, 1.34, 2.62, 3.41, 3.87, 4.02, 3.87, 3.41, 2.62, 1.34, 0]
result = scipy.trapz(fraction_of_span, rotation, 10)
print(result)
-2.6665
上述已构建解决方案的示例,使用simp

from scipy.integrate import simps
y = rotation
x = fraction_of_span


result_simps = simps(y, x)
print(result_simps)
2.6790000000000003
请注意,由于方法不同,结果非常相似,略有不同。注:符号应为正值,因为积分仅在正值之间(旋转元素均为正值)

你要问的内容中有很多很好的材料,也许你想看看这里:docs.scipy.org/doc/scipy/reference/tutorial/integrate.html

让我们尝试得到类似的矢量结果

要做到这一点,您可以转到上述函数并修改这些函数以获得结果。因此,我去修改/创建一个新函数,如下所示:

def trapz_modified(y, x=None, dx=1.0, axis=-1):
    """
    Integrate along the given axis using the composite trapezoidal rule.
    Integrate `y` (`x`) along given axis.
    Parameters
    ----------
    y : array_like
        Input array to integrate.
    x : array_like, optional
        The sample points corresponding to the `y` values. If `x` is None,
        the sample points are assumed to be evenly spaced `dx` apart. The
        default is None.
    dx : scalar, optional
        The spacing between sample points when `x` is None. The default is 1.
    axis : int, optional
        The axis along which to integrate.
    Returns
    -------
    trapz : float
        Definite integral as approximated by trapezoidal rule.
    See Also
    --------
    sum, cumsum
    Notes
    -----
    Image [2]_ illustrates trapezoidal rule -- y-axis locations of points
    will be taken from `y` array, by default x-axis distances between
    points will be 1.0, alternatively they can be provided with `x` array
    or with `dx` scalar.  Return value will be equal to combined area under
    the red lines.
    References
    ----------
    .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Trapezoidal_rule
    .. [2] Illustration image:
           https://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png
    Examples
    --------
    >>> np.trapz([1,2,3])
    4.0
    >>> np.trapz([1,2,3], x=[4,6,8])
    8.0
    >>> np.trapz([1,2,3], dx=2)
    8.0
    >>> a = np.arange(6).reshape(2, 3)
    >>> a
    array([[0, 1, 2],
           [3, 4, 5]])
    >>> np.trapz(a, axis=0)
    array([1.5, 2.5, 3.5])
    >>> np.trapz(a, axis=1)
    array([2.,  8.])
    """
    y = asanyarray(y)
    if x is None:
        d = dx
    else:
        x = asanyarray(x)
        if x.ndim == 1:
            d = diff(x)
            # reshape to correct shape
            shape = [1]*y.ndim
            shape[axis] = d.shape[0]
            d = d.reshape(shape)
        else:
            d = diff(x, axis=axis)
    nd = y.ndim
    slice1 = [slice(None)]*nd
    slice2 = [slice(None)]*nd
    slice1[axis] = slice(1, None)
    slice2[axis] = slice(None, -1)
    try:
        # MODIFIED HERE
        #ret = (d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0).sum(axis)
        ret = d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0
    except ValueError:
        # Operations didn't work, cast to ndarray
        d = np.asarray(d)
        y = np.asarray(y)
        # MODIFIED HERE
        #ret = add.reduce(d * (y[tuple(slice1)]+y[tuple(slice2)])/2.0, axis)
        ret = d * (y[tuple(slice1)]+y[tuple(slice2)])/2.0

    return ret
在文件/脚本的顶部,我们还需要以下库:

from numpy import diff
from numpy import asanyarray
让我们看看输出:

>>>trapz_modified(y, x=x)
array([0.0835, 0.198 , 0.3015, 0.364 , 0.3945, 0.3945, 0.364 , 0.3015,
       0.198 , 0.067 ])

在上面的测试中,你想要这个区域,因为你在谈论积分,但是包括了slope这个词,人们可能会想到派生词。在你所问的问题中有很好的材料,也许你想看看这里:谢谢你的回答,在梁理论中,旋转积分导致梁的斜率,双积分导致梁的偏转。这不应该与派生结合起来!谢谢你的回复。我宁愿采取每一个步骤,并整合和显示一个列表的结果感谢您的解决方案,正是它,我所寻求的!。
>>>trapz_modified(y, x=x)
array([0.0835, 0.198 , 0.3015, 0.364 , 0.3945, 0.3945, 0.364 , 0.3015,
       0.198 , 0.067 ])