【问题标题】:Scipy with numerical integration on multiple lists在多个列表上进行数值积分的 Scipy
【发布时间】:2021-05-04 18:50:17
【问题描述】:

我有一个图表,其中包含跨度步长和相应的旋转值。我需要对每个步骤进行数值积分以获得斜率值。我想知道,因为 scipy 集成中已经有内置函数,如梯形规则或辛普森规则。如果没有任何附加功能,如何在两个数组或数据列表上实现?

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)

预期结果:

result = [x0, x1, .........xn]

【问题讨论】:

  • 在上面的测试中,您需要该区域,因为您在谈论集成,但是包含了斜率这个词,人们可能会想到推导。你要的东西里面有好材料,也许你想看看这里:docs.scipy.org/doc/scipy/reference/tutorial/integrate.html
  • 谢谢回复,在光束理论方面,旋转积分导致光束倾斜,双积分导致光束偏转。这不应该与派生结合!

标签: python python-3.x scipy numerical-integration


【解决方案1】:

如上述提议

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

上面已经构建的解决方案示例,简单

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

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

为此,您可以转到上述功能并修改它们以获得结果。所以我去https://github.com/numpy/numpy/blob/master/numpy/lib/function_base.py#L4081-L4169并修改/创建一个新的功能如下:

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 ])

【讨论】:

  • 感谢您的回复。我宁愿采取每个步骤并整合并用列表显示结果
  • 感谢您的解决方案,正是我所寻求的!
猜你喜欢
  • 2018-05-10
  • 2013-12-16
  • 2018-08-26
  • 2016-05-16
  • 2014-11-27
  • 2023-04-10
  • 2013-08-24
  • 2012-06-04
  • 2013-01-29
相关资源
最近更新 更多