【问题标题】:How to do bounds to solve double integral for data arrays? Python如何做边界来解决数据数组的双积分? Python
【发布时间】:2018-07-16 03:43:18
【问题描述】:

Double integral with function and sampled data Python

参考这个问题,同样,我需要为一个我不知道生成它的函数的采样数据求解一个双积分。我已经了解这个答案如何解决此类问题。

但是,在我的情况下,数据高于某个区域,该区域取决于我也作为数组拥有的额外参数。举个例子,我现在假设它以圆形区域为界。

a,b 和 r 是常数,d 是参数列表。我希望列表的结果具有相同的 d 长度。出于某种原因,我想将其保留在笛卡尔坐标系中。

数据始终覆盖移动区域。我正在考虑削减数据,以便将部分保留在该区域中,然后使用问题中的解决方案。我想如果我总是(小于)/在界限内限制数据区域,它可能会导致大错误。

numpy 包中是否有更好的函数来解决数据数组的定双积分而不是使用 np.trapz?

【问题讨论】:

    标签: python numpy integral


    【解决方案1】:

    下面的代码使用来自scipy librarynquad(),并基于相关方程和示例hereresultabserr(绝对错误)存储在数据帧中。

    导入库

    import pandas as pd
    import numpy as np
    from scipy import integrate
    

    为全局变量设置值

    a = 0
    b = 10
    r = 10
    d_list = [1,2,3,4,5]
    

    定义函数

    def f(x, y):
        return x*y
    
    def bounds_y():
        return [a, b]
    
    def bounds_x(y):
        return [0, np.sqrt(r**2 - (y-d)**2)]
    

    迭代 'd' 中的值

    temp = []
    for i in d_list:
        d = i # re-set global value of 'd'
        temp.append(integrate.nquad(f, [bounds_x, bounds_y]))
    

    将结果和错误值保存在数据框中

    temp = pd.DataFrame(temp, columns=['result', 'abserr'])
    temp
    

    【讨论】:

    • 感谢您的回答。我认为这仅适用于已定义的函数,但我希望在一组数据点下找到体积。
    猜你喜欢
    • 2015-08-25
    • 2023-01-02
    • 2015-01-20
    • 2018-12-16
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2014-10-02
    • 2017-10-02
    相关资源
    最近更新 更多