【问题标题】:Returning the results of an integral as a function将积分的结果作为函数返回
【发布时间】:2021-07-22 20:40:22
【问题描述】:

我正在尝试计算一个函数的积分,其中实现了另一个积分的结果。它看起来有点像以下:

import numpy as np
import scipy.integrate as integrate

Formula_U=lambda t: 325 * np.sin(100 * np.pi * t -3)
Formula_I=lambda t: 100 * np.sin(100 * np.pi * t -3)

#To calculate:
Integral on [0,1] of ( Formula_I * INTEGRATED(Formula_U) )

Results = integrate.quad(Formula_I * ~~integrate.quad(Formula_U)~~ ,0,1)[0]

~~...~~ 之间的部分不起作用。我知道integrate.quad 不支持这一点,但我正在寻找将积分公式“Formula_U”返回为formula 而不是值的东西,因此可以动态计算给定的积分。

我已经尝试查看其他帖子,但我找不到任何关于无限制计算公式积分的内容。我尝试使用 lambda 将积分硬编码为一个单独的变量,但只要我在方程中添加一些谐波正弦波,输入正弦波就会发生变化,所以这不是一个选项。

我尝试过使用 sympy 做一些事情,但我不知道如何使用 lambda 将字符串转换为变量。

提前致谢!

【问题讨论】:

  • 您可以将 Formula_U 定义为对象函数而不是 lambda,并将其传递给integrate.quad

标签: python math scipy sympy integral


【解决方案1】:

您是否打算传递 lambda 或表达式进行积分?如果你定义一个像f = lambda x: 1 + cos(x) 这样的lambda 如果你写f(2) 你会得到1 + cos(2);如果你只写f,你就会得到函数本身,准备好调用其他东西,例如g=f; g(2) == f(2)-->True。绕过 scipy 并在 SymPy 中执行此操作如下所示:

>>> from sympy import *
>>> from sympy.abc import t
>>> Formula_U=lambda t: 325 * sin(100 * pi * t -3)
>>> Formula_I=lambda t: 100 * sin(100 * pi * t -3)
>>> general = integrate(Formula_U(t))
>>> integrate(Formula_I(t) * general, (t,0,1))
0

【讨论】:

    【解决方案2】:

    我无法确定,因为我没有使用 scipy 集成的经验,但我认为您可以尝试以下方法:

    import numpy as np
    import scipy.integrate as integrate
    
    #Formula_U=lambda t: 325 * np.sin(100 * np.pi * t -3)
    Formula_I=lambda t: 100 * np.sin(100 * np.pi * t -3)
    
    def Formula_U(t)
        325 * np.sin(100 * np.pi * t -3)
    
    #To calculate:
    #Integral on [0,1] of ( Formula_I * INTEGRATED(Formula_U) )
    
    Results = integrate.quad(Formula_I,Formula_U) ,0,1)[0]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多